I am working on application where I have to replace a file(image captured from camera) after editing it. Priorly I send request using Intent Sender to delete located at some URI and after that I save the image at that URI, but in Android 13 after deletion , when I open outputStream of deleted file to write new content It gives FileNotFoundException.
val intentSender = MediaStore.createDeleteRequest(contentResolver, uriList).intentSender deleteRequestResult.launch(IntentSenderRequest.Builder(intentSender).build())
and onResultOfDelete
context.contentResolver.openOutputStream(outputUri, "wt")?.use { output -> val buffer = ByteArray(4096) var length: Int while (inputStream.read(buffer).also { length = it } > 0) output.write(buffer, 0, length) output.flush() }
what when I openOutputStream it crashes and gives FileNotFoundException.
Starting from Android 11, apps can no longer access files outside their own app-specific directories by default. To fix this issue, you can use ContentResolver.insert
to create a new file in your app's directory and get a new URI for that file