File conversion: // scheme for content: // scheme

I have a problem using the Droid X Files app and Astro file manager to select an image file. These two applications return the selected image with the "file: //" scheme, and the Gallery returns the image with the "content: //" scheme. How to convert the first scheme to the second. Or how can I decode an image with a second format?

+4
source share
3 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 
+3
source

Use ContentResolver.openInputStream () or related methods to access the byte stream. Normally you should not worry about whether it is a file or content: a URI.

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri )

+3
source

The problem here is that for all files we cannot have Uri content (content: //). Because uri content is for those files that are part of MediaStore . For example: images, audio and video.

However, for supported files, we can find its absolute path. As for the images as follows:

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

The general 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; } }} 
+3
source

All Articles