Reputation: 5711
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.jjdj.jdjjd.jdjjdj/sdcard1/storage/emulated/0/Pictures/Paintings/Painting_440.png from pid=27901, uid=1000 requires the provider be exported, or grantUriPermission() at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:740) at android.content.ContentProvider.semEnforceReadPermission(ContentProvider.java:659) at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602) at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:593) at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:507) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:307) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994)
Below code has been used for this:
private fun share(fileName: String) {
val shareFile = File(getStoragePath(), fileName)
val contentUri = FileProvider.getUriForFile(activity, SHARED_PROVIDER, shareFile)
val intentBuilder = contentUri?.let {
ShareCompat.IntentBuilder.from(activity)
.setType("image/*")
.addStream(it)
}
val chooserIntent = intentBuilder?.createChooserIntent()
activity.startActivity(chooserIntent)
}
provider passed in manifest :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.jsjsj"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Path Xml :
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="Paintings"
path="Paintings/" />
<external-path
name="Paintings"
path="." />
<external-files-path
name="Paintings"
path="." />
<!-- FOR SD CARD-->
<root-path
name="sdcard1"
path="." />
</paths>
Upvotes: 2
Views: 2744
Reputation: 143
I had a similar problem. This was what I ended up doing, it's set up a little differently than yours but hopefully its helpful.
val myBitmap = //create a bitmap here
val file = File(activity.externalCacheDir, "myFileName.png")
val fileOutputStream = FileOutputStream(file)
myBitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutputStream)
fileOutputStream.flush()
fileOutputStream.close()
val fileProviderUri = FileProvider.getUriForFile(context, context.applicationContext.packageName
+ ".provider", file);
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
flags = Intent.FLAG_ACTIVITY_NEW_TASK
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
putExtra(Intent.EXTRA_STREAM, fileProviderUri)
type = "image/png"
}
val shareIntent = Intent.createChooser(sendIntent, null)
if (shareIntent.resolveActivity(context.packageManager) != null) {
context.startActivity(shareIntent)
} else {
Toast.makeText(context, "Error Occurred Please Try Again", Toast.LENGTH_SHORT).show()
}
Manifest provider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Upvotes: 2