1

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

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

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

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

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

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

4

2 回答 2

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列存储文件路径,因此您可以使用该信息来查找它。MediaStore

于 2012-07-22T20:49:09.833 回答
1

回答我自己的问题:)

找到了一种简单而简单(!)的方法来查找路径的存储:**

    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);          
    }
于 2011-02-18T20:47:03.080 回答