Android 暂时保存图片并在不再保存时删除它们的最佳做法?

Android best practice to save images temporarily and remove them when no longer?

我目前正在开发 android 应用程序。在这个应用程序中,我从远程服务器下载图像以便在其中显示它们。实际上,我将这些文件保存在 sdcard/emulated/0/myapp 中。我需要的是如何在用户使用 myapp 时使这些图像暂时可用,并且应该在用户按照本期最佳实践关闭应用程序时将其删除。我将图像保存到 sdcard 的代码是:

public static String saveToSdCard(Bitmap bitmap, String filename) {

    String stored = null;

    File sdcard = Environment.getExternalStorageDirectory();
    File folder = new File(sdcard.getAbsoluteFile(), "myapp");
    if (!folder.exists()) {
        folder.mkdirs();
    }
    File file = new File(folder.getAbsoluteFile(), filename + ".jpeg");
    if(file.exists()){
        file.delete();
    }
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    /*if (file.exists())
        return stored;
    */
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
        stored = "success";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stored;
}

提前致谢。

将它们存储在缓存目录中。 getCacheDir() 函数找到的缓存目录。

您可以使用 Picasso 缓存功能吗?试试这个 link -> https://futurestud.io/tutorials/picasso-influencing-image-caching