Saturday, 3 April 2021

Android 11 URI usage (file:// content://)

We have some old issues with similar words, but most of them are about converting one or the other.

What I'm looking here is the "Right" behaviour of URI usage with the new changes. Let me give some context:

Before when we get an image URI this would return file://... format. But since the new OS permissions changes, where we should not use WRITE_EXTERNAL_STORAGE anymore we should use getUriForFile(..) that return content://... path.(Scope Storage usage Android 11 Storage FAQ)

This can be spot on some Android guides, like: taken photos guide

The "problem" is that many users got used to use the URI of a crop image (for example) to create a file of it and save it.

Now, with this changes come the question:

How should we use the URI?

  1. Make some code to check Android version and if more than 29 we should create a new file path for the URI?
  2. Let the URI be the path to the image (content of file) and if someone wanna save it would need to create it own file path
  3. Something else that I don't get yet about how to use URI right.

Obs: Asking this, because of a Android Image Crop open source project handover, where we need to upgrade the permissions for Android 10/11 but now we have this content/file issue. More here

Edit: As pointed on the comments

Code returning file:// (not valid anymore since the changes)

Uri outputFileUri = null;
outputFileUri = Uri.fromFile(
                  new File(context.getExternalCacheDir().getPath(), 
                  "pickImageResult.jpeg")
                );
    

Code returning content://

outputFileUri = FileProvider.getUriForFile(
                        context,
                        context.getPackageName() + CommonValues.authority,
                        File.createTempFile("pickImageResult", ".jpeg", getImage)
                );


from Android 11 URI usage (file:// content://)

No comments:

Post a Comment