Last active
June 23, 2025 22:43
-
-
Save Demon000/d1a194ba9f07190908b1591e8c91b69f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cn.nubia.camera.utils; | |
| import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE; | |
| import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; | |
| import android.content.ClipData; | |
| import android.content.ContentResolver; | |
| import android.content.ContentUris; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.database.Cursor; | |
| import android.net.Uri; | |
| import android.provider.MediaStore; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class CameraUtil { | |
| private static long[] getMediaIdsInSecureModeWithType(Context context, long startTime, long endTime, int type) { | |
| ContentResolver resolver = context.getContentResolver(); | |
| Uri queryUri = MediaStore.Files.getContentUri("external"); | |
| String[] projection = new String[] { "_id" }; | |
| String selection = "(media_type=?) AND date_modified between ? and ?"; | |
| String sortOrder = "datetaken DESC"; | |
| String[] selectionArgs = new String[3]; | |
| selectionArgs[0] = String.valueOf(type); | |
| selectionArgs[1] = String.valueOf(startTime / 1000); | |
| selectionArgs[2] = String.valueOf(endTime / 1000); | |
| Cursor cursor = resolver.query(queryUri, projection, selection, selectionArgs, sortOrder); | |
| long[] resultIds = new long[]{}; | |
| if (cursor == null) { | |
| return resultIds; | |
| } | |
| try { | |
| if (cursor.moveToFirst()) { | |
| int count = cursor.getCount(); | |
| resultIds = new long[count]; | |
| int index = 0; | |
| do { | |
| long id = cursor.getLong(0); // getLong of "_id" column | |
| resultIds[index] = id; | |
| index++; | |
| } while (cursor.moveToNext()); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| cursor.close(); | |
| } | |
| return resultIds; | |
| } | |
| public static void addClipData(Intent intent, Context context, long firstCreateTime, long currentTime) { | |
| List<Uri> uris = new ArrayList<>(); | |
| long[] imageIds = getMediaIdsInSecureModeWithType(context, firstCreateTime, currentTime, MEDIA_TYPE_IMAGE); | |
| for (long imageId : imageIds) { | |
| Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId); | |
| uris.add(uri); | |
| } | |
| long[] videoIds = getMediaIdsInSecureModeWithType(context, firstCreateTime, currentTime, MEDIA_TYPE_VIDEO); | |
| for (long videoId : videoIds) { | |
| Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoId); | |
| uris.add(uri); | |
| } | |
| ContentResolver contentResolver = context.getContentResolver(); | |
| ClipData clipData = null; | |
| for (int i = 0; i < uris.size(); i++) { | |
| Uri uri = uris.get(i); | |
| if (i == 0) { | |
| clipData = ClipData.newUri(contentResolver, null, uri); | |
| } else { | |
| clipData.addItem(contentResolver, new ClipData.Item(uri)); | |
| } | |
| } | |
| if (clipData != null) { | |
| intent.setClipData(clipData); | |
| } | |
| } | |
| public static void viewUriInGallery(Uri thumbnailUri, Context context, boolean isSecure, long firstCreateTime, long currentTime) { | |
| Intent intent = new Intent(); | |
| if (isSecure) { | |
| intent.setAction(MediaStore.ACTION_REVIEW_SECURE); | |
| } else { | |
| intent.setAction(MediaStore.ACTION_REVIEW); | |
| } | |
| intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
| if (isSecure) { | |
| addClipData(intent, context, firstCreateTime, currentTime); | |
| } | |
| intent.setDataAndType(thumbnailUri, "image/jpeg"); | |
| try { | |
| context.startActivity(intent); | |
| } catch (android.content.ActivityNotFoundException e) { | |
| // ignore | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment