Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

兼容Android 11 #90

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14)
[![jitPack](https://jitpack.io/v/jaiselrahman/FilePicker.svg)](https://jitpack.io/#jaiselrahman/FilePicker)
[![Monthly Downloads](https://jitpack.io/v/jaiselrahman/FilePicker/month.svg)](https://jitpack.io/#jaiselrahman/FilePicker)
[![Weekly Downloads](https://jitpack.io/v/jaiselrahman/FilePicker/week.svg)](https://jitpack.io/#jaiselrahman/FilePicker)
[![](https://jitpack.io/v/fuzhengyin/FilePicker.svg)](https://jitpack.io/#fuzhengyin/FilePicker)
[![Monthly Downloads](https://jitpack.io/v/jaiselrahman/FilePicker/month.svg)](https://jitpack.io/#fuzhengyin/FilePicker)
[![Weekly Downloads](https://jitpack.io/v/jaiselrahman/FilePicker/week.svg)](https://jitpack.io/#fuzhengyin/FilePicker)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-FilePicker-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/7002)

A FilePicker library for Android for selecting multiple types of files and also to capture Images and Videos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ public void onClick(View v) {
.setCheckPermission(true)
.setShowImages(false)
.setShowVideos(false)
.setIgnoreHiddenFile(false)
.setIgnoreNoMedia(false)
.setShowAudios(true)
.setSingleChoiceMode(true)
.setSuffixes("mp3")
.setSelectedMediaFile(file)
.setTitle("Select an audio")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.MimeTypeMap;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class FilePickerActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

long current = System.currentTimeMillis();
configs = getIntent().getParcelableExtra(CONFIGS);
if (configs == null) {
configs = new Configurations.Builder().build();
Expand Down Expand Up @@ -169,6 +170,7 @@ public boolean isAutoMeasureEnabled() {
if (maxCount > 0) {
setTitle(getResources().getString(title_res, fileGalleryAdapter.getSelectedItemCount(), maxCount, title));
}
Log.i(TAG, "onCreate: ms:"+(System.currentTimeMillis() - current));
}

private boolean useDocumentUi() {
Expand All @@ -191,6 +193,7 @@ public void onChanged(PagedList<MediaFile> mediaFiles) {

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = [" + permissions + "], grantResults = [" + grantResults + "]");
if (requestCode == REQUEST_WRITE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadFiles();
Expand All @@ -212,6 +215,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
if (requestCode == FileGalleryAdapter.CAPTURE_IMAGE_VIDEO) {
if (resultCode == RESULT_OK) {
String path = fileGalleryAdapter.getLastCapturedFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public Configurations[] newArray(int size) {
}
};

public static int PAGE_SIZE = 120;
public static int PREFETCH_DISTANCE = 40;
public static int PAGE_SIZE = 30;
public static int PREFETCH_DISTANCE = 10;

private final boolean imageCaptureEnabled;
private final boolean videoCaptureEnabled;
Expand Down Expand Up @@ -345,6 +345,11 @@ public Builder setRootPath(String rootPath) {
return this;
}

/**
* 根据后缀名过滤
* @param suffixes 不需要在后缀名前面添加`.` 内部会自动添加
* @return
*/
public Builder setSuffixes(String... suffixes) {
this.suffixes = suffixes;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void refresh() {
}

public static class Factory extends ViewModelProvider.NewInstanceFactory {
private ContentResolver contentResolver;
private Configurations configs;
private final ContentResolver contentResolver;
private final Configurations configs;

public Factory(ContentResolver contentResolver, Configurations configs) {
this.contentResolver = contentResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContentResolverCompat;
import androidx.paging.DataSource;
import androidx.paging.PositionalDataSource;
Expand All @@ -47,14 +50,17 @@

public class MediaFileDataSource extends PositionalDataSource<MediaFile> {

private Configurations configs;
private ContentResolver contentResolver;
private final Configurations configs;
private final ContentResolver contentResolver;

private String[] projection;
private String sortOrder;
private String selection;
private String[] selectionArgs;
private Uri uri;
private final String[] projection;
private final String sortOrder;

private final String sortColumn;
private final int sortDirection;
private final String selection;
private final String[] selectionArgs;
private final Uri uri;

private MediaFileDataSource(ContentResolver contentResolver, Uri uri, @NonNull Configurations configs, Long dirId) {
this.contentResolver = contentResolver;
Expand Down Expand Up @@ -114,6 +120,18 @@ private MediaFileDataSource(ContentResolver contentResolver, Uri uri, @NonNull C
selectionBuilder.append(") and ");
}

if (configs.isShowAudios()) {
String[] suffixes = configs.getSuffixes();
if (suffixes.length > 0) {
selectionBuilder.append("(");
for (String suffix : suffixes) {
selectionBuilder.append(DATA).append(" like '%.").append(suffix).append("' or ");
}
selectionBuilder.delete(selectionBuilder.length()-4, selectionBuilder.length());
selectionBuilder.append(") AND ");
}
}

if (configs.isSkipZeroSizeFiles()) {
selectionBuilder.append(SIZE).append(" > 0 ");
}
Expand All @@ -127,7 +145,6 @@ private MediaFileDataSource(ContentResolver contentResolver, Uri uri, @NonNull C
if (canUseAlbumId(configs)) {
projection.add(MediaStore.Audio.AudioColumns.ALBUM_ID);
}

List<String> folders = getFoldersToIgnore(contentResolver, configs);
if (folders.size() > 0) {
selectionBuilder.append(" and(").append(DATA).append(" NOT LIKE ? ");
Expand All @@ -139,11 +156,13 @@ private MediaFileDataSource(ContentResolver contentResolver, Uri uri, @NonNull C
}
selectionBuilder.append(")");
}

this.projection = projection.toArray(new String[0]);
this.sortColumn = DATE_ADDED;
this.sortOrder = DATE_ADDED + " DESC";
this.sortDirection =ContentResolver.QUERY_SORT_DIRECTION_DESCENDING;
this.selection = selectionBuilder.toString();
this.selectionArgs = selectionArgs.toArray(new String[0]);

}

@Override
Expand All @@ -155,8 +174,25 @@ public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialC
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<MediaFile> callback) {
callback.onResult(getMediaFiles(params.startPosition, params.loadSize));
}

@RequiresApi(api = Build.VERSION_CODES.O)
private Bundle create(int limit, int offset) {
Bundle bundle = new Bundle();
bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, limit);
bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, offset);
if (sortColumn !=null && sortColumn.trim().length() != 0) {
bundle.putString(ContentResolver.QUERY_ARG_SORT_COLUMNS, sortColumn);
bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, sortDirection);
}
bundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection);
bundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
return bundle;
}
private List<MediaFile> getMediaFiles(int offset, int limit) {
Log.i("Fuzhengyin", "getMediaFiles: offset:"+offset+" limit:"+limit);
if (android.os.Build.VERSION.SDK_INT >= 30) {
Cursor query = contentResolver.query(uri, projection, create(limit, offset), null);
return MediaFileLoader.asMediaFiles(query, configs);
}
Cursor data = ContentResolverCompat.query(contentResolver, uri, projection,
selection, selectionArgs,
sortOrder + " LIMIT " + limit + " OFFSET " + offset, null);
Expand All @@ -176,6 +212,9 @@ private static boolean canUseMediaType(Configurations configs) {

@NonNull
private static List<String> getFoldersToIgnore(ContentResolver contentResolver, Configurations configs) {
if (!configs.isIgnoreHiddenFile() && !configs.isIgnoreNoMediaDir() && configs.getIgnorePathMatchers() == null) {
return new ArrayList<>();
}
Uri uri = MediaStore.Files.getContentUri("external");

String[] projection = new String[]{DATA};
Expand Down Expand Up @@ -257,11 +296,11 @@ private static boolean isExcluded(String path, List<String> ignoredPaths) {
}

public static class Factory extends DataSource.Factory<Integer, MediaFile> {
private ContentResolver contentResolver;
private Configurations configs;
private Long dirId;
private final ContentResolver contentResolver;
private final Configurations configs;
private final Long dirId;

private Uri uri;
private final Uri uri;

Factory(ContentResolver contentResolver, Configurations configs, Long dirId) {
this.contentResolver = contentResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -81,6 +82,7 @@ static Uri getContentUri(Configurations configs) {
}

static List<MediaFile> asMediaFiles(Cursor data, Configurations configs) {
long current = System.currentTimeMillis();
ArrayList<MediaFile> mediaFiles = new ArrayList<>(data.getCount());
if (data.moveToFirst())
do {
Expand All @@ -89,6 +91,7 @@ static List<MediaFile> asMediaFiles(Cursor data, Configurations configs) {
mediaFiles.add(mediaFile);
}
} while (data.moveToNext());
Log.i("Fuzhengyin", "asMediaFiles: ms:"+(System.currentTimeMillis() - current));
return mediaFiles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import com.jaiselrahman.filepicker.config.Configurations;

public class MediaFileViewModel extends ViewModel {
private ContentResolver contentResolver;
private final ContentResolver contentResolver;
public LiveData<PagedList<MediaFile>> mediaFiles;

private ContentObserver contentObserver = new ContentObserver(null) {
private final ContentObserver contentObserver = new ContentObserver(null) {
@Override
public void onChange(boolean selfChange) {
refresh();
Expand Down Expand Up @@ -69,9 +69,9 @@ public void refresh() {
}

public static class Factory extends ViewModelProvider.NewInstanceFactory {
private ContentResolver contentResolver;
private Configurations configs;
private Long dirId;
private final ContentResolver contentResolver;
private final Configurations configs;
private final Long dirId;

public Factory(ContentResolver contentResolver, Configurations configs, Long dirId) {
this.contentResolver = contentResolver;
Expand Down