【发布时间】:2018-11-09 04:13:31
【问题描述】:
所以我正在做一个项目,如果经理注册用户,他会收到一封带有 QR 码(位图)的电子邮件。二维码保存在缓存中。我希望在将 QR 码发送给用户后删除 QR 码,但是会创建一个“缓存”文件夹(也显示在图库中),并且图像本身会被删除但仍保留在那里(您看不到它,但它那里是一个灰色的正方形)。 知道如何完全删除创建的文件夹和创建的位图吗?
我的代码:
BitmapSaver(Context mContext){
this.mContext=mContext;
this.cache = new DiskBasedCache(mContext.getCacheDir(), 1024 * 1024);
}
public static File saveImageToExternalStorage(Context context, Bitmap finalBitmap) {
destFolder =context.getCacheDir().getAbsolutePath();
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
// myDir = new File(root + "/saved_images");
// myDir.mkdirs();
long n = System.currentTimeMillis() / 1000L;
fname = "Image-" + n + ".jpg";
//file2 = new File(destFolder);
file = new File(destFolder+"/"+fname);
if (file.exists())
file.delete();
try {
Log.i("path",destFolder+"/"+fname);
FileOutputStream out = new FileOutputStream(destFolder+"/"+fname);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
url = Uri.parse(path);
Log.i("External",url.toString());
}
});
return file;
}
发送电子邮件后的活动中:
BitmapSaver bms = new BitmapSaver(RegisterActivity.this);
bms.saveImageToExternalStorage(RegisterActivity.this, bitmap);
bms.file.delete();
【问题讨论】:
-
删除后应该用MediaScannerConnection重新扫描文件,实际上添加时不需要扫描文件,因为它会通知设备该图像已准备好被其他应用程序使用.
-
谢谢,成功了。
-
查看我的回答了解详细信息
标签: android caching bitmap delete-file