question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Media provider Uri permission problem

See original GitHub issue

Version info

  • Android Upload Service version: 3.2.3
  • Android version and API version: 19+ & targetSdkVersion 25
  • HTTP stack (e.g. HurlStack or OkHttpStack): OkHttpStack

What did you expect?

The upload service to be able to read a content://com.android.providers.media.documents/ Uri

What happened instead?

A permission problem when trying to query the Cursor : Caused by

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image:47 from pid=14195, uid=10076 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

Library initialization code:

public BinaryUploadRequest setFileToUpload(final Uri file, final String fileName) throws FileNotFoundException {
        this.fileName = fileName;

        /* ------- DEBUG -------
        Cursor cursor = context.getContentResolver().query(file, null, null, null, null);
        if (cursor == null) {
            Logger.error(getClass().getSimpleName(), "null cursor for " + file + ", returning size 0");
        }
        int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
        cursor.moveToFirst();
        long size = cursor.getLong(sizeIndex);
        cursor.close();
        Log.d(TAG, "setFileToUpload: Successfully retrieved file size: "+size);
        /* ----- END DEBUG ----- */

        super.context.grantUriPermission("net.gotev.uploadservice", file, Intent.FLAG_GRANT_READ_URI_PERMISSION);

        String fullUrl = serverUrl+path+fileName;
        Log.d(TAG, "Updating URL to "+fullUrl);
        this.params.setServerUrl(fullUrl);
        realm.executeTransaction(...);

        return super.setFileToUpload(Uri.decode(file.toString()));
  • [Commented code] this code is causing the problem when used in the runnable part of the upload service but works here as my app has all the needed permissions to read a content Uri.
  • As you can see I tried to grantUriPermission() in vain

Request code:

MyCustomBinaryUploadRequest(getContext(), UUID.randomUUID(), currentUserInstance.serverURL, currentUserInstance.currentAbsoluteDirPath(), null)
.setFileToUpload(fileUri, fileName)
.startUpload();

Where have you added the request code?

  • Activity
  • Service
  • Other class (add additional info about it)

LogCat output (please set log level to DEBUG first)

D/MyCustomBinaryUploadRequest: Upload f29f61c7-0eaf-4ebe-a0dd-137171bc37e8 registered in DB for user drive5650
D/MyCustomBinaryUploadRequest: Updating URL to https://drive5650.phpnetstorage.eu/IMG_20170518_163139.jpg
D/MyCustomBinaryUploadRequest: Registering file to upload for request f29f61c7-0eaf-4ebe-a0dd-137171bc37e8 in DB for user drive5650 (File path on device : content://com.android.providers.media.documents/document/image%3A47 Remote path : /)
D/MyCustomBinaryUploadRequest: Registered file to upload for request f29f61c7-0eaf-4ebe-a0dd-137171bc37e8) in DB for user drive5650
D/AcceuilActivity: Launched upload of file content://com.android.providers.media.documents/document/image:47 to https://drive5650.phpnetstorage.eu/. (UploadID : f29f61c7-0eaf-4ebe-a0dd-137171bc37e8)
D/AcceuilActivity: Registering the uploadservice status broadcast listener.
I/UploadService: UploadService - Starting service with namespace: net.gotev, upload pool size: 2, 1s idle thread keep alive time. Foreground execution is enabled
D/UploadService: UploadService - Successfully created new task with class: net.gotev.uploadservice.BinaryUploadTask
D/UploadService: UploadService - f29f61c7-0eaf-4ebe-a0dd-137171bc37e8 now holds the foreground notification
D/UploadService: HttpUploadTask - Starting upload task with ID f29f61c7-0eaf-4ebe-a0dd-137171bc37e8
I/UploadService: UploadTask - Broadcasting error for upload with ID: f29f61c7-0eaf-4ebe-a0dd-137171bc37e8. Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image:47 from pid=14195, uid=10076 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

Server side

Server side is an Apache 2 (WebDav)

More info

  • I also tried in vain adding permissions to the uploadservice module <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
secu2commented, May 19, 2017

My app has all the permissions requested:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
[...]

I even retrieve the file name with no problem before starting the service :

Cursor cursor = getActivity().getContentResolver().query(fileUri, null, null, null, null, null);
if(cursor != null && cursor.moveToFirst()){
    String fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}

The READ_EXTERNAL_STORAGE permission is requested at runtime and granted successfully just before launching the file chooser.

1reaction
gajicm93commented, May 26, 2017

Fixed it!

The problem is with the Intent you pick up content with, and whether you properly take read permission for Uri. Here is the solution if anybody needs it:

https://stackoverflow.com/questions/25414352/how-to-persist-permission-in-android-api-19-kitkat/29588566#29588566

Read more comments on GitHub >

github_iconTop Results From Across the Web

Permission Denial: reading com.android ... - Stack Overflow
I added the permission on the manifest.xml. But I still having the same error: Permission Denial: reading com.android.providers.media.MediaProvider.
Read more >
<grant-uri-permission> | Android Developers
If a content provider's grantUriPermissions attribute is " true ", permission can be granted for any the data under the provider's purview. However,...
Read more >
Need help getting video from Gallery - Xamarin.Android - MSDN
Error message says: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from ...
Read more >
Accessing the Camera and Stored Media - CodePath Cliffnotes
ACTION_IMAGE_CAPTURE); // Create a File reference for future access photoFile = getPhotoFileUri(photoFileName); // wrap File object into a content provider ...
Read more >
Sharing files through Intents (part 2): fixing the permissions ...
If you've updated your app to drop the old file:// Uri and you've been trying to ... SecurityException: Permission Denial: opening provider ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found