0

我正在尝试检索有关存储在 sdcard 中的用户视频的信息,我有这个方法:

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_TAKEN、MediaStore.Video.Media.DURATION、MediaStore.Video.Media.SIZE 和 MediaStore.Video.Media.RESOLUTION,我总是得到“0”,因为对于这些,我总是抓住非法参数异常。

为什么?

4

2 回答 2

0

The values are 0 because that is the default and they were never set to be anything else. The MediaStore does not automatically calculate these values when a video is copied to sdcard.

For duration you could try using MediaPlayer to load the video and then call getDuration.

于 2013-04-10T08:14:29.883 回答
0

此代码从一个特殊的音频返回信息。但它确实有效。您可以根据我的主要问题 whit while 语句更改它以获取所有音频或视频。您必须在我对 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();

也请参阅此链接: 通过 mediastore 从文件中获取您的信息

于 2013-11-22T08:30:10.167 回答