Last active
August 14, 2023 11:42
-
-
Save vuhung3990/72ca35c814dad9258a53 to your computer and use it in GitHub Desktop.
get all video and image file on device
This file contains 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
/** | |
* get all video path on device | |
* permission: READ_EXTERNAL_STORAGE | |
* @param context | |
* @return list file media | |
*/ | |
public static List<File> getAllMediaFilesOnDevice(Context context) { | |
List<File> files = new ArrayList<>(); | |
try { | |
final String[] columns = { MediaStore.Images.Media.DATA, | |
MediaStore.Images.Media.DATE_ADDED, | |
MediaStore.Images.Media.BUCKET_ID, | |
MediaStore.Images.Media.BUCKET_DISPLAY_NAME }; | |
MergeCursor cursor = new MergeCursor(new Cursor[]{context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null), | |
context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null, null), | |
context.getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, columns, null, null, null), | |
context.getContentResolver().query(MediaStore.Video.Media.INTERNAL_CONTENT_URI, columns, null, null, null) | |
}); | |
cursor.moveToFirst(); | |
files.clear(); | |
while (!cursor.isAfterLast()){ | |
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); | |
int lastPoint = path.lastIndexOf("."); | |
path = path.substring(0, lastPoint) + path.substring(lastPoint).toLowerCase(); | |
files.add(new File(path)); | |
cursor.moveToNext(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment