Skip to content Skip to sidebar Skip to footer

Get A Content Uri From A File Uri?

I am using the DownloadManager to download an image to the system's gallery and then in the Broadcast receiver (once the download succeeds) using an Intent to set the image as the

Solution 1:

I was able to figure it out. It was a combination of the code found here: Converting android image URI and scanning the media file after downloading.

So after the file finished downloading I get the path and do the following:

StringuriString= c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

//Update the SystemUriu= Uri.parse(uriString);                       
context.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, u));

//Get the abs path using a file, this is important          Filewallpaper_file=newFile(u.getPath());
UricontentURI= getImageContentUri(context, wallpaper_file.getAbsolutePath());

For some reason starting the media scanner, newing the file, and getting the absolute path are important, I'm not exactly sure why but I can't spend any more time on this!

The way to convert from a file URI to a content URI is as follows (taken from the linked StackOver flow post:

publicstatic Uri getImageContentUri(Context context, String absPath) {
    Log.v(TAG, "getImageContentUri: " + absPath);

    Cursor cursor = context.getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        , newString[] { MediaStore.Images.Media._ID }
        , MediaStore.Images.Media.DATA + "=? "
        , newString[] { absPath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , Integer.toString(id));

    } elseif (!absPath.isEmpty()) {
         ContentValues values = new ContentValues();
         values.put(MediaStore.Images.Media.DATA, absPath);
         return context.getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
        returnnull;
    }
}

Maybe this will help someone in the future.

Solution 2:

So my question is if anyone knows how to convert a full file path/URI (file://) into a content style URI (content://)?

Implement a ContentProvider. FileProvider offers an out-of-the-box solution for serving up local files.

Solution 3:

I'm not sure about the technique you are using to set the wallpaper but the easiest way is probably to use WallpaperManager.setStream() which doesn't require any URI.

Also note that a file URI only works between apps if the file is publicly accessible so a content URI is a more general solution.

Using a content URI implies that a ContentProvider will serve the file. Which one depends on where your file is located.

If your app has a direct read access to the file, you can implement a content provider in your app by using for example the FileProvider class of the support library, but this should really only be used if the file is located in the private data storage of your app.

If the image is added to the system media gallery, you should probably use the URI provided by the MediaStore.

Post a Comment for "Get A Content Uri From A File Uri?"