简体   繁体   中英

How to delete pictures in Android External Storage Public Directory at uninstall?

卸载应用程序时,如何删除保存在外部存储公共目录Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)图片。

You should save your data in External Cache bqz you cannot delete any file after uninstall. External cache will be deleted with your app. selectAvailableCacheDir will provide you file object of available cache.

static File selectAvailableCacheDir(Context context) {
        for (File file : ContextCompat.getExternalCacheDirs(context)) {
            if (file != null && ensureDirExists(file))
                return file;
        }
        return null;
    }

    static Boolean ensureDirExists(File dirFile) {
        if (!dirFile.exists()) {
            return dirFile.mkdir() || dirFile.mkdirs();
        } else if (dirFile.exists() && dirFile.isDirectory())
            return true;
        return false;
    }

I don't know what you want to achieve actually.

But When an application is uninstalled, all of its components are uninstalled (this includes any services, content providers, etc.). The system broadcast ACTION_PACKAGE_REMOVED is made after the application has been removed, so there is no way that this application can get it.

Adding to @SRB bans answer,

You can do that by using a Broadcast Receiver.

Hope it helps.

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