6

Edit: This is not a duplicate question. No question I've seen answers how to solve this when you don't have control over the activity sending the intent (in my case, a browser app or maybe a file-browsing app is sending the intent to my app). And then more specifically, this is not dealing with photos/gallery.


This has been plaguing an app of mine for a while. I can't personally get it to happen with any device, but I can see from crashes it happens a lot to others.

My app receives an intent containing a ZIP file from an outside app. I catch it in either onCreate() or onNewIntent():

Intent intent = getIntent();
if (intent != null && intent.getData() != null)
    beginZipIntent(intent);

In beginZipIntent():

Uri data = intent.getData();
String filename = data.toString();

// Open input stream to copy ZIP to a temporary directory.
Uri uri = Uri.parse(filename);
InputStream inputStream = null;

try
{
    inputStream = getContentResolver().openInputStream(uri); // This fails
}
catch (Exception e)
{
    //...
}

On the line above, some devices fail:

Permission Denial: reading com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/2772 from pid=26094, uid=10094 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

I have no control over the app/activity sending the intent. I thought by grabbing the file immediately and saving it to a temporary directory I could remedy this (as seen in other answers) - but nope.

I've also added android.permission.MANAGE_DOCUMENTS but as expected (from other answers) it doesn't work.

Anyone ever run into this? Seems to affect devices ranging from Android 4 to 7 so not specific to one OS version.

yesbutmaybeno
  • 1,078
  • 13
  • 31

3 Answers3

4

Well I faced this issue before, I was have an oppo device which run on android 7.1 and it's worked fine btw it's make a problem on samsung with the same version of android, so to solve this issue I asked for the read storage permission if needed, and it's worked.

Example for the people who love code:

public class CheckPermissions {

public static boolean hasPermission(int PERMISSION_REQUEST, String permission, Context context) {
    if (ContextCompat.checkSelfPermission(context,
            permission)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                permission) &&
                ContextCompat.checkSelfPermission(context,
                        permission)
                        != PackageManager.PERMISSION_GRANTED) {

            return false;

        } else {

            ActivityCompat.requestPermissions((Activity) context,
                    new String[]{permission},
                    PERMISSION_REQUEST);

        }

        return false;
    } else {
        return true;


    }
    }
}

And to check the permission:

if (CheckPermissions.hasPermission(REQUEST_CODE,
                    Manifest.permission.READ_EXTERNAL_STORAGE, this))
                    // todo read the saved URI

Also I expected from you to add the permission to the manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And use the write permission to write the file too.

3

You may need to add following runtime permissions in your code and manifest also

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

you can refer this link #save_zip_files

I hope it helps!!

Anu Bhalla
  • 422
  • 4
  • 11
0

Just add this line before get URI

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Shubham Vala
  • 1,024
  • 7
  • 18