Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.SecurityException: Permission Denial #9

Open
BYVoid opened this issue Mar 5, 2020 · 19 comments
Open

java.lang.SecurityException: Permission Denial #9

BYVoid opened this issue Mar 5, 2020 · 19 comments

Comments

@BYVoid
Copy link

BYVoid commented Mar 5, 2020

I added androidx.core.content.FileProvider and provider_paths.xml, but still encountered such an error:

E/DatabaseUtils(22545): Writing exception to parcel
E/DatabaseUtils(22545): java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.app.provider/external_files/Android/data/com.example.app/files/test.json from pid=22857, uid=1000 requires the provider be exported, or grantUriPermission()
E/DatabaseUtils(22545): 	at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
E/DatabaseUtils(22545): 	at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
E/DatabaseUtils(22545): 	at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
E/DatabaseUtils(22545): 	at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
E/DatabaseUtils(22545): 	at android.os.Binder.execTransactInternal(Binder.java:1021)
E/DatabaseUtils(22545): 	at android.os.Binder.execTransact(Binder.java:994)
@lubritto
Copy link
Owner

Are you still getting this error?

@JenniferJesuraj
Copy link

@lubritto I'm seeing this exception on the console, but the functionality seems to work fine. Is this something that has to be fixed on the plugin side?

E/DatabaseUtils(11210): java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://filePath from pid=16973, uid=1000 requires the provider be exported, or grantUriPermission()
E/DatabaseUtils(11210): 	at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
E/DatabaseUtils(11210): 	at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
E/DatabaseUtils(11210): 	at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
E/DatabaseUtils(11210): 	at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
E/DatabaseUtils(11210): 	at android.os.Binder.execTransactInternal(Binder.java:1021)
E/DatabaseUtils(11210): 	at android.os.Binder.execTransact(Binder.java:994)

@longravuth
Copy link

I get it too on Android 10

@BYVoid
Copy link
Author

BYVoid commented Apr 15, 2020

Still reproducible.

@marcLeeroy
Copy link

I have the same issue but the feature works, i can share no problem but i get that warning in the console.

@vertis
Copy link

vertis commented Jun 23, 2020

I am seeing the same error:

For reference I found:

https://stackoverflow.com/questions/45893294/permission-denial-with-file-provider-through-intent

and

https://stackoverflow.com/questions/57689792/permission-denial-while-sharing-file-with-fileprovider

Specifically, the stackoverflow solution involves adding:

Intent chooser = Intent.createChooser(intentShareFile, "Share File");

List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    this.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

startActivity(chooser);

I am going to create a fork and see if this solves the problem. Will open a pull request if it does.

@vertis
Copy link

vertis commented Jun 23, 2020

If anyone is interested in using this bugfix until it is merged you can do so by adding the following to pubspec.yaml:

flutter_share:
    git:
      url: https://github.com/vertis/flutter_share.git

Just make sure to change it back once it's merged. I do not intend to keep my fork updated.

@rjaylwar
Copy link

rjaylwar commented Jul 2, 2020

Same issue.

@mobabur94
Copy link

I see the error logged in the console as well, although the feature seems to work. Would like for the fix to be merged in

@LokeshNakka
Copy link

LokeshNakka commented Dec 21, 2020

https://stackoverflow.com/a/65342993/14844728

Intent sharableIntent = new Intent();
sharableIntent.setAction(Intent.ACTION_SEND);
sharableIntent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION );

Uri imageUri = Uri.parse(ImgLoc);
File imageFile = new File(String.valueOf(imageUri));
Uri UriImage = FileProvider.getUriForFile(context, Author, imageFile);

sharableIntent.setType("image/*");
sharableIntent.putExtra(Intent.EXTRA_STREAM, UriImage);
sharableIntent.putExtra(Intent.EXTRA_TITLE, title);
sharableIntent.putExtra(Intent.EXTRA_TEXT, body);

Intent chooser = Intent.createChooser(sharableIntent, "Chooser Title");
chooser.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
context.startActivity(chooser);

@VivekThummar52
Copy link

I get it too on Android 10

i got it on Android 11, not on Android 10

@shubhamnandanwar
Copy link

I am getting in Android 10 and Android 12. (Not checked for Android 11)

@manwithsteelnerves
Copy link

@VivekThummar52 @shubhamnandanwar Did you find a solution?

@VivekThummar52
Copy link

VivekThummar52 commented Aug 9, 2021

@manwithsteelnerves
As of i know and i have tried, given solution worked for me, please try this :

try {
                    Uri uri = imagesList.get(position).getUri();
                    if (uri != null) {
                        String path = FileUtils.getPath(this, uri);
                        if (path == null) {
                            Toast.makeText(this, "Error getting Image Path", Toast.LENGTH_SHORT).show();
                            return false;
                        }
                        File shareFile = new File(path);
                        Uri shareUri = null;
                        try {
                            shareUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", shareFile); // ".provider" or ".fileprovider" or whatever you have mentioned in manifest file
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        if (shareUri != null) {
                            Intent shareIntent = getShareIntent(null, shareUri, "image/*");
                            startActivity(Intent.createChooser(shareIntent, "Share Image Using"));
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

public Intent getShareIntent(String packagename, Uri uri, String type) {
    Intent intent = new Intent("android.intent.action.SEND");
    if (packagename != null) {
        intent.setPackage(packagename);
    }

    if (Build.VERSION.SDK_INT > 23) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(uri, type);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
    } else {
        intent.setType(type);
    }

    intent.putExtra(Intent.EXTRA_SUBJECT, "Created by Your App Name");
    intent.putExtra(Intent.EXTRA_TEXT, Helper.shareText);
    intent.putExtra("android.intent.extra.STREAM", uri);
    return intent;
}

And tell me if this will work for you or not..

@manwithsteelnerves
Copy link

manwithsteelnerves commented Aug 9, 2021

Even I'm granting the read uri permission but not working for multiple files

It started working after adding the uri permission flag on the intent chooser too for all the attachments (uris).

List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            for (FileAttachment each:attachments) {
                context.grantUriPermission(packageName, each.uri,  Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }

@VivekThummar52
Copy link

@manwithsteelnerves
in line - intent.putExtra(Intent.EXTRA_STREAM, uri);, i think you can also set list of String or Uri, you have tried it or not??

if not then look at this answer - https://stackoverflow.com/a/15577579

@manwithsteelnerves
Copy link

@manwithsteelnerves
in line - intent.putExtra(Intent.EXTRA_STREAM, uri);, i think you can also set list of String or Uri, you have tried it or not??

if not then look at this answer - https://stackoverflow.com/a/15577579

Yes. I'm currently setting the array of streams(uri) only. But still it expects me to add the read permission flags on "chooser" intent by going through all of the matching packages (note you may need to add QUERY_ALL_PACKAGES or tag if >= API 30 is your target api)

@shubhamnandanwar
Copy link

shubhamnandanwar commented Aug 10, 2021

Hey,

I was using scoped storage and I figured it's impossible for me to share that particular image (please correct me if I am wrong). I found a thread in which the developer was creating a cache image in the scoped folder and was sharing the cached image. The developer also hardcoded the image name, so whenever a user shares a new image, the previous cache image gets overwritten. Unfortunately, I couldn't find that thread again but here's the code -


val cachePath = File(externalCacheDir, "my_images/")
cachePath.mkdirs()
val bitmap = loadImageFromStorage(currentQuote.bookId)
val file = File(cachePath, "readmore-cache.png")
val fileOutputStream: FileOutputStream
try {
	fileOutputStream = FileOutputStream(file)
	bitmap?.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
	fileOutputStream.flush()
	fileOutputStream.close()
} catch (e: FileNotFoundException) {
	e.printStackTrace()
} catch (e: IOException) {
	e.printStackTrace()
}
val cacheImageUri: Uri = FileProvider.getUriForFile(this, applicationContext.packageName + ".provider", file)
			
val intent = Intent(Intent.ACTION_SEND).apply {
	clipData = ClipData.newRawUri(null, cacheImageUri)
	putExtra(Intent.EXTRA_STREAM, cacheImageUri)
	type = "image/ *"
	addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(intent, null))


I have tested it in Android 12 and 10 and it's working fine.

@nicop2000
Copy link

I am still getting the error. Is there an update in the pipe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests