I want to get all videos path in android (Internal and External storage both), I have tried use:
List<String> paths = new ArrayList<String>();
File directory = new File("/system" OR "/mnt/sdcard");
File[] files = directory.listFiles();
for (int i = 0; i < files.length; ++i) {
if(files[i].getAbsolutePath().contains(".mp4")) {
paths.add(files[i].getAbsolutePath());
}
}
but I can not get all of video lists from my device.
Here is your solution must try if you want to really a actual result.
public ArrayList<String> getAllMedia() {
HashSet<String> videoItemHashSet = new HashSet<>();
String[] projection = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME};
Cursor cursor = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
try {
cursor.moveToFirst();
do{
videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
}while(cursor.moveToNext());
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
return downloadedList;
}
You need to make your search recursive. Something like:
void findVideos(File dir, ArrayList<String> list){
for (File file : dir.listFiles()) {
if (file.isDirectory()) findVideos(file, list);
else if(file.getAbsolutePath().contains(".mp4")) list.add(file.getAbsolutePath());
}
}
List<VideoClass> videoItems = new ArrayList<VideoClass>();
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
//looping through all rows and adding to list
if (cursor != null && cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
Uri contentUri = ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)));
Bitmap vimage =
null;
try {
vimage = getApplicationContext().getContentResolver().loadThumbnail(
contentUri, new Size(640, 480), null);
} catch (IOException e) {
e.printStackTrace();
}
VideoClass videoModel = new VideoClass();
videoModel.setTitle(title);
videoModel.setMain_video(String.valueOf(contentUri));
videoModel.setImage(vimage);
videoItems.add(videoModel);
} while (cursor.moveToNext());
}
return videoItems;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With