Skip to content

Commit

Permalink
增加排序方式,增加线程
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhengyin committed Sep 23, 2021
1 parent d81b1f2 commit 767d784
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
11 changes: 10 additions & 1 deletion matisse/src/main/java/com/zhihu/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class SelectionCreator {
MediaStore.MediaColumns.DATE_ADDED
})
@interface ORDERBYTIME{}
public SelectionCreator orderBy(String orderByP) {
public SelectionCreator orderBy(@ORDERBYTIME String orderByP) {
mSelectionSpec.orderBy = orderByP;
return this;
}
Expand All @@ -84,6 +84,15 @@ public SelectionCreator refresh(boolean refresh) {
return this;
}

@Retention(RetentionPolicy.SOURCE)
@StringDef({
"DESC","ASC"
})
@interface SortOrder{}
public SelectionCreator sortOrder(String sortOrder){
mSelectionSpec.sortOrder = sortOrder;
return this;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@IntDef({
SCREEN_ORIENTATION_UNSPECIFIED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.pm.ActivityInfo;
import android.provider.MediaStore;

import androidx.annotation.StringDef;
import androidx.annotation.StyleRes;

import com.zhihu.matisse.MimeType;
Expand All @@ -30,6 +31,8 @@
import com.zhihu.matisse.listener.OnCheckedListener;
import com.zhihu.matisse.listener.OnSelectedListener;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -61,6 +64,7 @@ public final class SelectionSpec {
public OnCheckedListener onCheckedListener;
public boolean showPreview;
public String orderBy;
public String sortOrder;
public boolean refresh;

private SelectionSpec() {
Expand Down Expand Up @@ -100,6 +104,7 @@ private void reset() {
originalMaxSize = Integer.MAX_VALUE;
showPreview = true;
orderBy = MediaStore.MediaColumns.DATE_ADDED;
sortOrder = "DESC";
refresh = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static String[] getSelectionAlbumArgsForGifType(int mediaType, String al
private final boolean mEnableCapture;

private AlbumMediaLoader(Context context, String selection, String[] selectionArgs, boolean capture) {
super(context, QUERY_URI, PROJECTION, selection, selectionArgs, SelectionSpec.getInstance().orderBy);
super(context, QUERY_URI, PROJECTION, selection, selectionArgs, SelectionSpec.getInstance().orderBy+" "+SelectionSpec.getInstance().sortOrder);
mEnableCapture = capture;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

public class MediaSelectionFragment extends Fragment implements
AlbumMediaCollection.AlbumMediaCallbacks, AlbumMediaAdapter.CheckStateListener,
Expand Down Expand Up @@ -98,6 +99,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
String.valueOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE),
String.valueOf(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO),
};
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.MediaColumns.SIZE,
MediaStore.Files.FileColumns.DATA
};
Uri external = MediaStore.Files.getContentUri("external");
String selection = "(" + MediaStore.Files.FileColumns.MEDIA_TYPE + "=?"
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "=?)";

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -125,34 +136,32 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mAlbumMediaCollection.create(requireActivity(), this);
mAlbumMediaCollection.load(album, selectionSpec.capture);

String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.MediaColumns.SIZE,
MediaStore.Files.FileColumns.DATA
};
Uri external = MediaStore.Files.getContentUri("external");
String selection = "(" + MediaStore.Files.FileColumns.MEDIA_TYPE + "=?"
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "=?)";
//更新相册
if (SelectionSpec.getInstance().refresh) {
Cursor query = getContext().getContentResolver().query(external, projection, selection, SELECTION_ALL_ARGS, null);
if (query!=null){
List<String> zeroSize = new ArrayList<>();
try {
while (query.moveToNext()) {
String name = query.getString(1);
int size=query.getInt(2);
if (size!=0) continue;
System.out.println(name + " " + query.getString(3));
zeroSize.add(query.getString(3));
new Thread(){
@Override
public void run() {
super.run();
Cursor query = getContext().getContentResolver().query(external, projection, selection, SELECTION_ALL_ARGS, null);
if (query!=null){
List<String> zeroSize = new ArrayList<>();
try {
while (query.moveToNext()) {
String name = query.getString(1);
int size=query.getInt(2);
if (size!=0) continue;
String path = query.getString(3);
System.out.println(name + " " + path);
zeroSize.add(path);
}
MediaScannerConnection.scanFile(requireContext(), zeroSize.toArray(new String[0]), null, (path, uri) -> {});
}finally {
query.close();
}
}
MediaScannerConnection.scanFile(requireContext(), zeroSize.toArray(new String[0]), null, (path, uri) -> {});
}finally {
query.close();
}
}
}.start();

}
}

Expand Down

0 comments on commit 767d784

Please sign in to comment.