从文件路径获取MediaStore内容Uri?

时间:2011-02-18 20:02:43

标签: android uri mediastore

我有一个列出MediaStore中所有视频的应用程序。 (我需要内部存储和外部存储)。

基本上我有两个游标可以查询MediaStore.Video.Media.EXTERNAL_CONTENT_URI和MediaStore.Video.Media.INTERNAL_CONTENT_URI。

然后我使用MergeCursor合并这些查询并在ListView中显示CursorAdapter。

有时我想删除其中一个视频,但我需要“内容Uri”才能进行此操作,因为我无法确定所选视频的存储空间。

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

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

2 个答案:

答案 0 :(得分: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.Audio(等)替换为MediaStore.Video来用于音频或文件或其他类型的存储内容):

/**
 * 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;
}

基本上,DATA的{​​{1}}列(或您要查询的任何子部分)都会存储文件路径,因此您可以使用该信息进行查找。

答案 1 :(得分: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);          
    }