Android MediaStore没有';t告诉我视频文件的大小、分辨率、持续时间和日期

Android MediaStore没有';t告诉我视频文件的大小、分辨率、持续时间和日期,android,mediastore,android-mediascanner,Android,Mediastore,Android Mediascanner,我正在尝试检索存储在SD卡中的用户视频信息,我有以下方法: protected void addVideo () { cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null); if (cursor.moveToFirst()) { int i = 1; do {

我正在尝试检索存储在SD卡中的用户视频信息,我有以下方法:

protected void addVideo () {
    cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            mediaColumns, null, null, null);

    if (cursor.moveToFirst()) {

        int i = 1;



        do {

            VideoItem newVVI = new VideoItem();
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Video.Media._ID));
            newVVI.idthumb = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Video.Thumbnails._ID));
            Cursor thumbCursor = cr.query(
                    MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                    thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
                            + "=" + id, null, null);
            if (thumbCursor.moveToFirst()) {
                newVVI.thumbPath = thumbCursor.getString(thumbCursor
                        .getColumnIndex(MediaStore.Video.Thumbnails.DATA));

            }
            newVVI.filePath = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            newVVI.title = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
            try {
                newVVI.date = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                newVVI.date = "0";
            }
            try {
                newVVI.duration = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                newVVI.duration = "0";
            }
            try {
                newVVI.size = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                newVVI.size = "0";
            }
            try {
                newVVI.resolution = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                newVVI.resolution = "0";
            }

            newVVI.mimeType = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));

            newVVI.id = Integer.toString(i);
            ITEMS.add(newVVI);
            ITEM_MAP.put(newVVI.id, newVVI);
            i++;


        } while (cursor.moveToNext());

        cursor.close();


    } else {
        Log.d("TEST", ": else);
    }



}
问题是,对于MediaStore.Video.Media.DATE_take、MediaStore.Video.Media.DURATION、MediaStore.Video.Media.SIZE和MediaStore.Video.Media.RESOLUTION,我总是得到“0”,因为对于这些,我总是捕捉到IllegalArgumentException


为什么?

值为0,因为这是默认值,并且它们从未被设置为其他值。 将视频复制到SD卡时,MediaStore不会自动计算这些值


对于持续时间,您可以尝试使用加载视频,然后调用getDuration。

此代码从一个特殊音频返回信息。但它确实有效。 您可以根据我的主要问题更改它,同时声明获取所有音频或视频。 您必须在我对MediaStore.Video的响应中更改MediaStore.audio

String name="";
String duration="";
String path="";
Log.d("Loading file: " + filepath,"");

        // This returns us content://media/external/videos/media (or something like that)
        // I pass in "external" because that's the MediaStore's name for the external
        // storage on my device (the other possibility is "internal")
Uri audiosUri = MediaStore.Audio.Media.getContentUri("external");

Log.d("audiosUri = " + audiosUri.toString(),"");

String[] projection = {MediaStore.Audio.AudioColumns.DATA,MediaStore.Audio.AudioColumns.DISPLAY_NAME,MediaStore.Audio.AudioColumns.DURATION};

// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = managedQuery(audiosUri, projection, MediaStore.Audio.AudioColumns.DATA + " LIKE ?", new String[] { filepath }, null);
cursor.moveToFirst();

path =cursor.getString( cursor.getColumnIndex(projection[0]));
name=cursor.getString( cursor.getColumnIndex(projection[1]));
int iduration = cursor.getColumnIndex(projection[2]);
duration=CalTotalTime(Integer.valueOf(iduration));
Log.d("pathhhhhhhhhhhhhhhhhh", path);
Log.d("nameeeeeeeeeeeeeeeeeeeeeee", name);
Log.d("duration", duration);

cursor.close();
请参见此链接: