Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top