I am building a photo vault (that helps users hide photos) for Android 11 onwards since the birth of mighty "scoped storage" I am able to hide photos by just moving them to the app's private directory.
Now the problem arises when I want to share an image without moving it to the public (shared) directory.
I followed the implemented FileProvider, which converts the image path to URI but when I share the content URI via an intent, the following error pops up in logcat and the receiver application can not read the image.
Permission Denial: reading androidx.core.content.FileProvider uri content://com.androidbull.incognito.vaultreborn.provider/photos/Screenshot_20211221-105658.jpg from pid=10376, uid=1000 requires the provider be exported, or grantUriPermission()
Here is the code I wrote to perform sharing
val imagePath = File(this.filesDir, "photos")
val newImageFile = File(imagePath, currentImage.imageName)
val imageUri = FileProvider.getUriForFile(this, "$packageName.provider", newImageFile)
//
val shareContentIntent = Intent(Intent.ACTION_SEND)
shareContentIntent.data = imageUri
shareContentIntent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
startActivity(Intent.createChooser(shareContentIntent, null))
and just in case you are wondering here is the my provider in AndroidManifest.xml
<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/file_paths">
</meta-data>
</provider>
And finally the file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="photos"
path="photos/" />
</paths>
Can anyone please help me here?
Thank you :)
Okay, so it turned out that there was a bug in my head, not in the code. I was only testing it for the very first image that I moved into my vault, somehow this image was showing perfectly but had a file size of 0 bytes, so that was the problem. I tested other images and worked perfectly.
Thanks, everyone for paying attention :)