如何获取视频文件详细信息,例如。 Android中的持续时间?

时间:2012-09-12 21:25:47

标签: java android api

我很难从文件中获取特定的视频文件详细信息,以便持续时间等等。我目前所能做的就是获取cursor所有文件,然后逐个循环。

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();
                        }

但我需要访问特定文件的详细信息,因此我尝试创建URI,但由于返回的cursor始终为null而没有运气。我哪里出错?

                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
编辑:感谢下面的zapl答案,我在下面得到了这个代码(注意我修改了光标线),但是现在返回的光标不是null,但它仍然是空的,当我尝试读取cursor.getString(2)时我得到了没有。

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);

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

你的uri错了。媒体数据库不知道如何解析路径,因此您必须使用您期望的路径查询该项目。这类似于下面的代码(因为我在这里直接写了它可能包含错误)。

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);