2012-08-24 17 views
0

我试图检索存储在SD卡的用户的视频信息,而且我这个方法:MediaStore不给我的尺寸,分辨率,持续时间和日期的视频文件

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”,因为这些我总是赶上IllegalArgumentException。

为什么?

回答

0

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

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

0

这段代码从一个特殊的音频返回信息,但它真正的工作。 你可以根据我的主要问题惠特改变它,而声明获取所有的音频或视频。 您必须在对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(); 

看到这个链接也: get your info from files by mediastore