简体   繁体   中英

Android 6.0 - external storage files being deleted upon app uninstall

My app uses the DownloadManager to download files to a subdirectory of the device's Music folder.

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
...
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/MyStuff/song.mp3");
request.setDestinationUri(Uri.fromFile(file));

I have noticed that the files are being deleted when the app is uninstalled from a device running Marshmallow (this is not happening on older OS versions). Do you have any ideas about this?

Thanks

This is done by an internal class called DownloadReceiver and defined in the com.android.providers.downloads package manifest

<receiver android:name=".DownloadReceiver" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.intent.action.UID_REMOVED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

Here the android.intent.action.UID_REMOVED action catches the eye. It was introduced in Lollipop triggering a call to handleUidRemoved() performing

resolver.delete(ALL_DOWNLOADS_CONTENT_URI, Constants.UID + "=" + uid, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM