简体   繁体   English

卸载时如何删除Android外部存储公共目录中的图片?

[英]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. 您应该将数据保存在“外部缓存bqz”中,卸载后无法删除任何文件。 External cache will be deleted with your app. 外部缓存将与您的应用一起删除。 selectAvailableCacheDir will provide you file object of available cache. selectAvailableCacheDir将为您提供可用缓存的文件对象。

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. 删除应用程序后,将进行系统广播ACTION_PACKAGE_REMOVED,因此此应用程序无法获取它。

Adding to @SRB bans answer, 除了@SRB禁令答案外,

You can do that by using a Broadcast Receiver. 您可以使用广播接收器来做到这一点。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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