Skip to content

Commit

Permalink
解决在加载耗时过长的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhengyin committed Dec 1, 2021
1 parent d473370 commit db9ee6f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public void onClick(View v) {
.setCheckPermission(true)
.setShowImages(false)
.setShowVideos(false)
.setIgnoreHiddenFile(false)
.setIgnoreNoMedia(false)
.setShowAudios(true)
.setSingleChoiceMode(true)
.setSelectedMediaFile(file)
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
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 @@ -22,6 +22,7 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
Expand Down Expand Up @@ -49,17 +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 final String[] projection;
private final String sortOrder;

private String sortColumn;
private int sortDirection;
private String selection;
private String[] selectionArgs;
private Uri uri;
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 @@ -132,7 +133,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 @@ -144,13 +144,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 @@ -176,6 +176,7 @@ private Bundle create(int limit, int offset) {
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);
Expand All @@ -199,6 +200,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 @@ -280,11 +284,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

0 comments on commit db9ee6f

Please sign in to comment.