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

发布于 2024-10-18 14:30:39 字数 367 浏览 1 评论 0原文

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

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

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

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

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

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

I have an application that lists all videos in MediaStore. (I need both internal storage and external storage).

Basically I have two cursors which query MediaStore.Video.Media.EXTERNAL_CONTENT_URI and MediaStore.Video.Media.INTERNAL_CONTENT_URI.

Then I use a MergeCursor to merge these queries and display in a ListView with a CursorAdapter.

The thing is that sometimes I want to delete one of the videos, but I need the "content Uri" for this operation because I don't have a way to determine the storage of the selected video.

I have the full file path of the Video and the ID in MediaStore.

How can I get the "content Uri" from the file path/Uri?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-10-25 14:30:39

要回答标题中的问题:

我必须从 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;
}

基本上,DATAMediaStore 的 code> 列(或您正在查询的任何子部分)存储文件路径,因此您可以使用该信息来查找它。

To answer the question in the title:

I have to get the path from URIs and get the URI from paths in my app. The former:

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

The latter (which I do for videos, but can also be used for Audio or Files or other types of stored content by substituting MediaStore.Audio (etc) for 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;
}

Basically, the DATA column of MediaStore (or whichever sub-section of it you're querying) stores the file path, so you use that info to look it up.

时光暖心i 2024-10-25 14:30:39

回答我自己的问题:)

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

    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);          
    }

Answering my own question :)

Found an easy and trivial(!) way to find the storage of the path:**

    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);          
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文