I want to save an image in its full size. I use a file provider for this. I can take the picture, but I don't get any data in the onActivityResult function. The intent is null. What am I doing wrong?
provider_path.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path name="root" path="." />
</paths>
provider in AndroidManifest.xml:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.bkoubik.longlesstime.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
My Fragment Class:
fun capturePhoto(){
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val fileUri:File = createImageFile();
val photoURI = FileProvider.getUriForFile(
requireActivity(),
"com.bkoubik.longlesstime.fileprovider",
fileUri
)
intent.putExtra( MediaStore.EXTRA_OUTPUT, photoURI )
startActivityForResult(intent,IMAGE_REQUEST)
}
private fun createImageFile(): File {
val wrapper = ContextWrapper(requireContext())
var photoFile = wrapper.getDir(IMAGE_DIRECTORY,Context.MODE_PRIVATE)
photoFile = File(photoFile,"${UUID.randomUUID()}.jpg")
return photoFile
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null)
{
//data = null
val imgData = data.data!!
...
}
}
companion object {
private val IMAGE_DIRECTORY = "long_less"
}
Error:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.bkoubik.longlesstime/app_long_less/1219a0bd-c17e-4709-b880-1e4f549362f6.jpg

First of all, I'll strongly recommend you to use new contracts API which is also recommended by Google:
Than, when photo was taken from GUI, I'm getting photo data from provider like this (it's my separate camera request Activity). Also, I'm using external cache directory because on my Android 10.0 device data folder is not accessible (see Android docs)
P.S. Take a note about
grantUriPermissionmethod call on photo URIAnd this is how I reques photo from camera:
P.P.S. Quick note on why do I pass provider URI over Intent's extra - my camera routines are in separate library which is used in GUI-project So they can have different
BuildConfig.APPLICATION_ID. This allows me to use same library in many projects.