1

I currently use this method to effectively convert an image File into a content uri...

protected static Uri convertFileToContentUri(Context context, File file) throws Exception {

    ContentResolver cr = context.getContentResolver();
    String imagePath = file.getAbsolutePath();
    String imageName = null;
    String imageDescription = null;
    String uriString = MediaStore.Images.Media.insertImage(cr, imagePath, imageName, imageDescription);
    return Uri.parse(uriString); }

...but the problem is that it requires the android.permission.WRITE_EXTERNAL_STORAGE permission.

Is there any way to perform the same conversion without requiring that permission?

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253

1 Answers1

3

That does not "convert an image File into a content uri". That inserts an entry in the MediaStore ContentProvider, which happens to give you a Uri back. However, anything can now get at that content. This is akin to posting your personal contact information up on Pastebin, because you felt that you needed a URL, and then wondering why all of a sudden you are getting a bunch of really strange phone calls. The URL is a side-effect of publishing the personal contact information; the Uri is a side-effect of publishing the image.

If you want to serve a file via a ContentProvider, add a ContentProvider to your app. Depending on where your file resides, you may be able to use the FileProvider supplied by the Android Support package (support-v4 or support-v13). Or, roll your own streaming ContentProvider by overriding openFile().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, but did you not see I used the word `effectively`. – ban-geoengineering Dec 23 '14 at 18:46
  • @ban-geoengineering: Actually, I had. That does not change the characteristics of the issue with your approach, and it does not change the solution required to have a `content:` `Uri` resolving to something in your app's internal storage. Since, by definition, the only thing that has access to your app's internal storage is your app, and since, by definition, the only thing that handles a `content:` `Uri` is a `ContentProvider`, you will need to implement a `ContentProvider` in your app. – CommonsWare Dec 23 '14 at 19:21
  • OK, thanks. I agree a ContentProvider would be the ideal solution. I have already implemented one and it works in terms of saving and retrieving image data for displaying on the UI, but there is some issue when I use it to share the image via google plus (which is the reason I created this question). If you can help with this underlying issue, my question (with bounty) for it is here: http://stackoverflow.com/questions/27591480/android-google-plus-cannot-share-image-from-my-content-provider – ban-geoengineering Dec 23 '14 at 21:53