# Displaying Video file from storage with MediaStore in android Java

I Used MediaStore to scan and display media files from my storage using below code

private void displayAllVideos() {
Uri uri;
Cursor cursor;
int column_index_data,thum;

String absolutePathThumb;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};

final String orderBy = MediaStore.Video.Media.DATE_ADDED + " DESC";
cursor = getApplicationContext().getContentResolver().query(uri,projection,null,null,orderBy);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);

while (cursor.moveToNext()){
Log.d("VIDEO", cursor.getString(0));
absolutePathThumb = cursor.getString(column_index_data);
Uri thumbUri = Uri.fromFile(new File(absolutePathThumb));
//String cursorThumb = cursor.getString(thum);
String fileName = FilenameUtils.getBaseName(absolutePathThumb);
String extension = FilenameUtils.getExtension(absolutePathThumb);
String duration = getDuration(absolutePathThumb);
VideoModel videoModel = new VideoModel();
videoModel.setDuration(duration);
videoModel.setVideo_uri(thumbUri.toString());
videoModel.setVideo_path(absolutePathThumb);
videoModel.setVideo_name(fileName);
videoModel.setVideo_thumb(cursor.getString(thum));
if (extension!=null){
videoModel.setVideo_extension(extension);
}else {
videoModel.setVideo_extension("mp4");
}
if (duration!=null){
videoModel.setDuration(duration);
}else {
videoModel.setDuration("00:00");
}
}


with above code i can able to get and display video file, Is there anything i need to improve in my code.

Your help in need thanks in advance.android

There are no big issues with your method, but I noticed something that can be improved:

Variable declaration

In Java is not common to declare the variables at the beginning of the method:

Uri uri;
Cursor cursor;
int column_index_data,thum;

String absolutePathThumb;


Declare and initialize variables in the same place, it's easier to read and maintain.

By the way, videoAdapter is declared but never used, that's another reason why you should avoid doing this.

Query results not used

Here you configure the projection:

String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};


But after the query you never use the results for MediaStore.Video.Media.BUCKET_DISPLAY_NAME and MediaStore.Video.Media._ID.

Close the resources

The Cursor object which is returned from the query is a resouce that needs to be closed:

cursor = getApplicationContext().getContentResolver().query(uri,projection,null,null,orderBy);


To close it automatically use the try-with-resources statement:

try (Cursor cursor = getApplicationContext().getContentResolver().query(uri,projection,null,null,orderBy)) {
// ...
}


Camelcase instead of underscore

From the Java Naming Convention:

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

So instead of videoModel.setVideo_uri() use videoModel.setVideoUri() or even better videoModel.setUri(). Similarly for the other methods.

Duplicated assignment

videoModel.setDuration(duration) is called three times. Compute the value of duration first and add it to videoModel only once.

Ternary operator instead of if-else

Sometime using the ternary operator is better for readability. For example instead of:

if (extension!=null){
videoModel.setVideo_extension(extension);
}else {
videoModel.setVideo_extension("mp4");
}


You can get the same result with:

String extension = extension != null ? extension : "mp4";
videoModel.setVideo_extension(extension);


Documentation

In the link you posted there is an example that is very similar to your use case. Most of my suggestions are already mentioned there, read it carefully.

• hi, i was not able to retrieve some video files ?. any help – sanoj lawrence Sep 22 '20 at 2:59