spirytus
spirytus

Reputation: 10946

How to get video file details eg. duration in Android?

I struggle to get specific video file details so duration etc. from the file the files recorded earlier. All I can currently do is to get cursor with all the files, then loop one by one.

Cursor cursor = MediaStore.Video.query(getContext().getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.VideoColumns.DURATION,MediaStore.Video.VideoColumns.DATE_TAKEN,MediaStore.Video.VideoColumns.RESOLUTION,MediaStore.Video.VideoColumns.DISPLAY_NAME});


                    if(cursor.moveToFirst())
                        while(!cursor.isLast()){

                                if(cursor.getString(3)==fight.filename)
                                {
                                    // do something here
                                }

                                cursor.moveToNext();
                        }

I need however to access details of specific files so I tried to create URI but no luck as cursor returned is always null. Where do I go wrong?

                Uri uri = Uri.parse(Environment.DIRECTORY_DCIM+"/FightAll_BJJ_Scoring/"+(fight.filename));

                Cursor cursor = MediaStore.Video.query(getContext().getContentResolver(), uri, new String[]{MediaStore.Video.VideoColumns.DURATION,MediaStore.Video.VideoColumns.DATE_TAKEN,MediaStore.Video.VideoColumns.RESOLUTION,MediaStore.Video.VideoColumns.DISPLAY_NAME});

                // cursor is always null here

EDIT: thanks to zapl answer below I got this code below (note that I modified cursor line), however now cursor returned is not null, but its still empty and when I try to read cursor.getString(2) I got nothing.

Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    String selection = MediaColumns.DATA + "=?";
                    String[] selectionArgs = { Environment.DIRECTORY_DCIM+"/FightAll_BJJ_Scoring/"+(fight.filename) };
                    Cursor cursor = getContext().getContentResolver().query(uri, new String[]{MediaStore.Video.VideoColumns.DURATION,MediaStore.Video.VideoColumns.DATE_TAKEN,MediaStore.Video.VideoColumns.RESOLUTION,MediaStore.Video.VideoColumns.DISPLAY_NAME}, selection, selectionArgs, null);

Any help will be much appreciated.

Upvotes: 2

Views: 4222

Answers (1)

zapl
zapl

Reputation: 63955

Your uri is wrong. The media database does not know how to resolve a path so you must query the item with the path you expect. That works like in below code (which might contain errors since I wrote it directly here).

Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String selection = MediaColumns.DATA + "=?";
String dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();

String[] selectionArgs = { dcim+"/FightAll_BJJ_Scoring/"+(fight.filename) };

Cursor cursor = MediaStore.Video.query(getContext().getContentResolver(), uri, new String[]{MediaStore.Video.VideoColumns.DURATION,MediaStore.Video.VideoColumns.DATE_TAKEN,MediaStore.Video.VideoColumns.RESOLUTION,MediaStore.Video.VideoColumns.DISPLAY_NAME}, selection, selectionArgs, null);

Upvotes: 2

Related Questions