I'm trying to avoid TransactionTooLargeException by writing an image byteArray into a file created in
context.getExternalFilesDir(Environment.Directory_PICTURES), generating a URI from it, and passing the URI from the host app activity into another activity that resides outside of the host app module, perhaps from another dependency AAR. Once the 2nd activity is done retrieving the byteArray string from the file using the URI, I would like to delete the file as I don't have any more use for it and it is imperative that I delete it.
I modified the sample code for generating an image JPEG file here:
private fun createImageFile(flag: Boolean): File {
// Create an image file name
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val prefixName = if(flag) "StringA" else "StringB"
return File.createTempFile(
"JPEG_${prefixName}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
}
Here is my code for writing to the file.
val photoFile = createImageFile(flag).also {
photoURI = FileProvider.getUriForFile(this@AppHostActivity,"com.mydomain.fileprovider", it)
}
fileOutputStream = FileOutputStream(photoFile).apply {
this.write(imageByteArray)
}
I pass photoURI into an intent bundle to the next activity not in the host app module, and from this activity, I would like to delete the imageFile from the app's storage defined by context.getExternalFileDir(Environment.Directory_PICTURES).
And this is where I'm stuck right now.
I have yet to look into getCacheDir() or getExternalCacheDir(), or into MediaStore API. I'm targetting Android 13 as well and I believe there have been some restrictions to reading and writing app storage in Android around Android 10.
Can someone point me in the right direction for how to delete the imageFile when I'm not in the host activity module and with only the URI for the file?