2011-02-18 90 views
1

我有一个应用程序列出了MediaStore中的所有视频。 (我需要内部存储和外部存储)。从文件路径获取MediaStore内容Uri?

基本上我有两个游标,它们查询MediaStore.Video.Media.EXTERNAL_CONTENT_URI和MediaStore.Video.Media.INTERNAL_CONTENT_URI。

然后我使用一个MergeCursor来合并这些查询并显示在带有CursorAdapter的ListView中。

问题是,有时候我想删除其中一个视频,但是我需要这个操作的“内容Uri”,因为我没有办法确定所选视频的存储。

我有完整的文件路径的视频和ID在MediaStore。

如何从文件路径/ Uri获取“内容Uri”?

回答

2

回答我的问题:)

找到一个简单而琐碎的方式寻找路径的存储(!):**

if(selectedVideoPath.indexOf(Environment.getExternalStorageDirectory().getPath()) == 0) 
    { 
     getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
       MediaStore.Video.Media._ID + "=" + videoId, 
       null); 
    } 
    else { 
     getContentResolver().delete(MediaStore.Video.Media.INTERNAL_CONTENT_URI, 
       MediaStore.Video.Media._ID + "=" + videoId, 
       null);   
    } 
+1

我有文件:///并且需要内容://以及您从哪里获得ID? – sgarman 2011-04-08 01:45:09

4

要回答标题中的问题:

我必须从URI获取路径,并从我的应用中的路径获取URI。前者:

/** 
* Gets the corresponding path to a file from the given content:// URI 
* @param selectedVideoUri The content:// URI to find the file path from 
* @param contentResolver The content resolver to use to perform the query. 
* @return the file path as a string 
*/ 
private String getFilePathFromContentUri(Uri selectedVideoUri, 
     ContentResolver contentResolver) { 
    String filePath; 
    String[] filePathColumn = {MediaColumns.DATA}; 

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    filePath = cursor.getString(columnIndex); 
    cursor.close(); 
    return filePath; 
} 

后者(这是我对影片做的,但也可以通过对MediaStore.Video代MediaStore.Audio(ETC),用于音频或文件或其他类型的存储内容):

/** 
* Gets the MediaStore video ID of a given file on external storage 
* @param filePath The path (on external storage) of the file to resolve the ID of 
* @param contentResolver The content resolver to use to perform the query. 
* @return the video ID as a long 
*/ 
private long getVideoIdFromFilePath(String filePath, 
     ContentResolver contentResolver) { 


    long videoId; 
    Log.d(TAG,"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 videosUri = MediaStore.Video.Media.getContentUri("external"); 

    Log.d(TAG,"videosUri = " + videosUri.toString()); 

    String[] projection = {MediaStore.Video.VideoColumns._ID}; 

    // TODO This will break if we have no matching item in the MediaStore. 
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(projection[0]); 
    videoId = cursor.getLong(columnIndex); 

    Log.d(TAG,"Video ID is " + videoId); 
    cursor.close(); 
    return videoId; 
} 

基本上,MediaStoreDATA列(或任何它要查询的子部分)存储的文件路径,让你用这些信息来关注一下吧。