我删除了一个图像并在Android中缓存,但再次应用程序显示给我

时间:2017-08-03 16:46:27

标签: android image caching download

我在一个recyclerview中有物品,当我用HttpURLConnection库下载它时,保存那个有图像的物品并正确保存在我的手机中。 当我在我的应用程序中从手机中删除该项目时,它将被正确删除描述和图像

如果我用另一个图像保存该项目,但图像的名称相同,我看到它显示了我之前保存的第一个图像,即使我删除了它。

我没有代码来缓存我的图像,但我有一个代码来清理缓存文件夹 我测试了该代码,它清理缓存文件夹,但我还有这个问题。 在调试中,我发现缓存文件夹是空的,因为“for”没有运行,并且直接删除了缓存文件夹

除了缓存文件夹之外是否有我的android保存图像用于缓存的文件夹?

下载图片代码:

private Bitmap downloadUrl(String strUrl) throws IOException {
    Bitmap bitmap = null;
    InputStream iStream = null;
    File secondFile;
    Bitmap.CompressFormat format = null;
    try {
        File myDir = AdapterContext.getFilesDir();
        String Extention ="png"
        File NewsDirectory = new File("my app location and folder");
        if (!(NewsDirectory.isDirectory() && NewsDirectory.isDirectory())) {
            boolean success = (new File("check if folder is not made then it make it")).mkdir();
        }
        URL url = new URL(strUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        iStream = urlConnection.getInputStream();
        bitmap = BitmapFactory.decodeStream(iStream);
        secondFile = new File("my app folder"+"my file name" + Extention);
        ImageAddress = secondFile.toString();
        FileOutputStream stream = new FileOutputStream(secondFile);
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        format = Bitmap.CompressFormat.PNG;
        bitmap.compress(format, 85, outstream);
        byte[] byteArray = outstream.toByteArray();
        stream.write(byteArray);
        stream.close();
        ImageDownloaded = true;
    } catch (Exception e) {
    } finally {
        iStream.close();
    }
    return bitmap;
}

删除缓存代码:

File dir = Context.getCacheDir();
deleteDir(dir);
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
 return dir.delete();
} else {
return false;
}
}

1 个答案:

答案 0 :(得分:0)

guys my problem was solved with this code below just befor connection was open:

UrlConnection.setRequestProperty("Cache-Control", "no-cache");