Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting file:// scheme to content:// scheme

I am running to the problem with using Droid X's Files app and Astro file manager to select an image file. This two apps return the selected image with the scheme "file://" while Gallery returns the image with the scheme "content://". How do I convert the first schema to the second. Or how do I decode the image with the second format?

like image 890
KITT Avatar asked Jun 10 '11 01:06

KITT


People also ask

How do I change the path of a file in android?

File file = new File(Environment. getExternalStorageDirectory(), "read.me"); Uri uri = Uri. fromFile(file); File auxFile = new File(uri. toString()); assertEquals(file.

How do I change the path of a file in flutter?

You can use flutter_absolute_path to convert path to File().

What is a content URI?

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.


2 Answers

You probably want to convert content:// to file://

For gallery images, try something like this:

Uri myFileUri;
Cursor cursor = context.getContentResolver().query(uri,new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
if(cursor.moveToFirst())
{
    myFileUri = Uri.parse(cursor.getString(0)).getPath();
}
cursor.close
like image 149
cjb Avatar answered Oct 28 '22 10:10

cjb


Here, the problem is that, for all files we can't have a content Uri (content://). Because content uri is for those files which are part of MediaStore. Eg: images, audio & video.

However, for supported files, we can find its absolute path. Like for images as follows-

File myimageFile = new File(path);
Uri content_uri=getImageContentUri(this,myimageFile);

The generic method is as follows.

public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        new String[] { MediaStore.Images.Media._ID },
        MediaStore.Images.Media.DATA + "=? ",
        new String[] { filePath }, null);

if (cursor != null && cursor.moveToFirst()) {
    int id = cursor.getInt(cursor
            .getColumnIndex(MediaStore.MediaColumns._ID));
    Uri baseUri = Uri.parse("content://media/external/images/media");
    return Uri.withAppendedPath(baseUri, "" + id);
} else {
    if (imageFile.exists()) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, filePath);
        return context.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
        return null;
    }
}}
like image 23
Nizam Avatar answered Oct 28 '22 11:10

Nizam