Next code for deleting a file which my app owns works ok, there is no exception RecoverableSecurityException
because the file was created by my app (using ContentResolver.insert(...)
method)
getVideoFileContentUri(context, file)?.let { uri ->
try {
context.contentResolver.delete(uri, null, null)
} catch (securityException: RecoverableSecurityException) {
val intentSender =
securityException.userAction.actionIntent.intentSender
intentSender?.let {
activity.startIntentSenderForRecsult(
intentSender,
REQUEST_CODE,
null,
0,
0,
0,
null
)
}
}
}
fun getVideoFileContentUri(context: Context, file: File): Uri? {
val filePath = file.absolutePath
val cursor = context.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, arrayOf(MediaStore.Video.Media._ID),
MediaStore.Video.Media.DATA + "=? ", arrayOf(filePath), null
)
return if (cursor != null && cursor.moveToFirst()) {
val id: Int = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID))
cursor.close()
Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id)
} else {
null
}
}
But if I update a file created by app using ContentResolver.update(...)
method then deleting the file will require permission - it throws RecoverableSecurityException
and starts intent which opens a system dialog to confirm modifying the file
// here I change file name of the file
val contentValues = ContentValues(1).apply {
put(MediaStore.Video.Media.DISPLAY_NAME, "SOME NEW FILE NAME")
}
contentResolver.update(uri, contentValues, null, null)
So now it doesn't look like my app owns that file and for deleting it my users have to confirm deletion for each file
This is really annoying, how can I solve this problem?
So after ContentResolver.update(...)
for your own file created by ContentResolver.insert(...)
app loses permission for modifying this file and will require requesting it