Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete file on Android 11 (API 30) without system confirmation dialog?

I have an app which records videos to shared MOVIES folder.

I can delete those files on Android 11 (API 30) with contentResolver.delete(uri, null, null) method in my recorded videos activity.

But if I reinstall the app then it looses permissions to those files... (so afwul) and in such case I need to do something like this:

try {
    context.contentResolver.delete(uri, null, null)
} catch (exception: Exception) {
    if (exception is RecoverableSecurityException) {
        val intentSender = exception.userAction.actionIntent.intentSender
        intentSender?.let {
            callback?.startIntentSenderForResult(
                intentSender,
                requestCode
            )
        }
    }
}

So it couldn't delete the file using ContentResolver because app was reinstalled and there is exception which we can catch and open the next annoying dialog for a user to confirm deletion (and for each file deletion it should be a different dialog, multiple deleting - no way)

enter image description here

Then I installed Explorer app from Google Play on this Android 11 device (emulator), when I opened it the app only asked for storage write permission (my app also does it) and this Explorer app could easily delete any file (including my record videos files) without any confirmation dialog.

So how do they do it? Is it a hack or what is that?

Link to the app https://play.google.com/store/apps/details?id=com.speedsoftware.explorer

Update

VLC for Android can also delete any media file https://play.google.com/store/apps/details?id=org.videolan.vlc

They also use content provider, so it's the same but it returns true unlike my app, why?

fun deleteFile(file: File): Boolean {
    var deleted: Boolean
    //Delete from Android Medialib, for consistency with device MTP storing and other apps listing content:// media
    if (file.isDirectory) {
        deleted = true
        for (child in file.listFiles()) deleted = deleted and deleteFile(child)
        if (deleted) deleted = deleted and file.delete()
    } else {
        val cr = AppContextProvider.appContext.contentResolver
        try {
            deleted = cr.delete(MediaStore.Files.getContentUri("external"),
                    MediaStore.Files.FileColumns.DATA + "=?", arrayOf(file.path)) > 0
        } catch (ignored: IllegalArgumentException) {
            deleted = false
        } catch (ignored: SecurityException) {
            deleted = false
        }
        // Can happen on some devices...
        if (file.exists()) deleted = deleted or file.delete()
    }
    return deleted
}

https://github.com/videolan/vlc-android/blob/master/application/vlc-android/src/org/videolan/vlc/util/FileUtils.kt#L240

like image 889
user25 Avatar asked Oct 28 '20 22:10

user25


People also ask

How do I delete files without confirmation?

Right-click the Recycle Bin icon that is loaded onto your desktop by default and select Properties from the context menu. You should see something like Figure A. From that page you can toggle the delete confirmation by checking or unchecking the checkbox.

How do I delete other files in Android 11?

The Files app may also find large files taking up a significant amount of space; to review these, tap the button to Select files. Select any files you want to remove and then tap Delete (Figure B).

How to delete a file in Android 11?

In the android 11 file.delete () method only works if you create your own content. for example, Your application download one image and the location is external storage in this casework file.delete () method.

What is Android 11 API level 30?

Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. This release introduces several enhancements, such as raw file path access, batch edit operations for media, and an updated UI for the Storage Access Framework.

What are the restrictions of Android 11?

Android 11 expands upon this restriction. If your app targets Android 11, it cannot access the files in any other app's data directory, even if the other app targets Android 8.1 (API level 27) or lower and has made the files in its data directory world-readable. Access to app-specific directories on external storage

How to permanently delete private files on Android phone?

The most drastic way to permanently delete private files is to perform a factory reset. This will erase all the data on your phone, so is best used prior to selling or recycling your device. You should back up any Android data you want to keep beforehand.


1 Answers

Android 11 (API 30) without system confirmation dialog you can do but you need to manage_external_storage permission. The permission is allowed to some specific category application.

  • File managers
  • Backup and restore apps
  • Anti-virus apps
  • Document management apps
  • On-device file search
  • Disk and file encryption
  • Device-to-device data migration

Manage all files on a storage device

If your app do not follow the above category so you do not allow to publish with manage_external_storage permission.

If your application is a gallery, video, and audio player so you do not need to manage_external_storage permission and you can delete it directly with the system confirmation dialog.
Here you can get the example to delete media file

Before android 11 you can direct the used file.delete() method and delete your file.

In the android 11 file.delete() method only works if you create your own content. for example, Your application download one image and the location is external storage in this casework file.delete() method.

if you want to delete media files like camera or screenshot that time file.delete() method not work in android 11 because the media content you are not created. This situation follows with a system confirmation dialog.

like image 161
Jatin Borsaniya Avatar answered Oct 31 '22 16:10

Jatin Borsaniya