Last active
February 20, 2021 16:33
-
-
Save rahulshah456/c0db53e087ffc305f015f33012169162 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
| private List<MediaImage> queryStorage(String relativePath) { | |
| List<MediaImage> mediaImages = new ArrayList<>(); | |
| // query items | |
| String[] projection = new String[] { | |
| MediaStore.Images.Media._ID, | |
| MediaStore.Images.Media.DISPLAY_NAME, | |
| MediaStore.Images.Media.HEIGHT, | |
| MediaStore.Images.Media.WIDTH, | |
| MediaStore.Images.Media.MIME_TYPE, | |
| MediaStore.Images.Media.DATE_ADDED, | |
| MediaStore.Images.Media.RESOLUTION, | |
| MediaStore.Images.Media.SIZE, | |
| MediaStore.Images.Media.DATA, | |
| MediaStore.Images.Media.ISO, | |
| }; | |
| Uri collection; // query volume uri | |
| String selection; // sql query condition | |
| String[] selectionArgs; // sql conditional argument | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | |
| collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL); | |
| projection[projection.length - 1] = MediaStore.Images.Media.RELATIVE_PATH; | |
| selection = MediaStore.Images.Media.RELATIVE_PATH + " = ?"; | |
| selectionArgs = new String[] { relativePath }; | |
| } else { | |
| collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | |
| selection = MediaStore.Images.Media.DATA + " LIKE %?%"; | |
| selectionArgs = new String[] { "%" + relativePath + "%" }; | |
| } | |
| //ascending query order | |
| String sortOrder = MediaStore.Images.Media.DATE_ADDED + " ASC"; | |
| try (Cursor cursor = getApplicationContext().getContentResolver().query( | |
| collection, | |
| projection, | |
| selection, | |
| selectionArgs, | |
| sortOrder | |
| )) { | |
| // Cache column indices. | |
| int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); | |
| int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME); | |
| int heightColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.HEIGHT); | |
| int widthColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.WIDTH); | |
| int resColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.RESOLUTION); | |
| int mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE); | |
| int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED); | |
| int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE); | |
| int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); | |
| int relativeColumn = -1; | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | |
| relativeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.RELATIVE_PATH); | |
| } | |
| while (cursor.moveToNext()) { | |
| // created content uri based on the unique id | |
| long id = cursor.getLong(idColumn); | |
| Uri contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); | |
| // Stores column values and the contentUri in a local object | |
| mediaImages.add(new MediaImage( | |
| contentUri, | |
| cursor.getString(nameColumn), cursor.getInt(widthColumn), | |
| cursor.getInt(heightColumn), cursor.getString(resColumn), | |
| cursor.getString(mimeColumn), cursor.getString(dateColumn), | |
| cursor.getInt(sizeColumn), cursor.getString(dataColumn), | |
| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)? cursor.getString(relativeColumn): null) | |
| )); | |
| } | |
| } | |
| return mediaImages; | |
| } | |
| class MediaImage( | |
| val contentURI: Uri? = null, | |
| val name: String? = null, | |
| val width: Int = 0, | |
| val height: Int = 0, | |
| val resolution: String? = null, | |
| val mimeType: String? = null, | |
| val dateAdded: String? = null, | |
| val size: Int = 0, | |
| val absolutePath: String? = null, | |
| val relativePath: String? = null | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment