【发布时间】:2016-06-05 11:36:23
【问题描述】:
我想将我的位图保存到缓存目录。 我使用此代码:
try {
File file_d = new File(dir+"screenshot.jpg");
@SuppressWarnings("unused")
boolean deleted = file_d.delete();
} catch (Exception e) {
// TODO: handle exception
}
imagePath = new File(dir+"screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
它工作正常。但是,如果我想将不同的 img 保存到同一路径,就会出现问题。我的意思是它保存到相同的路径,但我看到它是旧图像,但是当我单击图像时,我可以看到我第二次保存的正确图像。
也许它来自缓存,但我不想看到旧图像,因为当我想与 whatsapp 看到的旧图像共享该图像时,如果我发送图像它似乎是正确的。
我想在 whatsapp 上分享保存的图像,如下代码:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagePath));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
我该如何解决?
提前致谢。
【问题讨论】:
-
最佳做法是在保存新图像时首先从目录中清除之前的图像(同名图像)。
-
我删除了你在我的代码中看到的上一个图像,它不起作用?
-
检查
deleted值。如果图像被删除,那么它将永远不会回来。 -
我调试了代码,我控制了它被删除的文件,但是当回来时,它之前点击的小图像是错误的
-
当您替换文件时,听起来缓存的缩略图没有被刷新。您应该寻找一种方法来刷新缓存。你在哪里显示图像?是在您的代码中还是在其他应用程序中?示例代码会很有用。
标签: android android-layout android-imageview android-bitmap android-file