【问题标题】:Temporary creating bitmaps in Android then deleting them在 Android 中临时创建位图然后删除它们
【发布时间】: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


【解决方案1】:

首先你需要知道MediaScannerConnection.scanFile 做了什么。它将文件信息更新到当前设备,因此画廊、文件浏览器等其他应用程序可以显示正确的文件信息和内容。

从您的代码中,当您保存临时文件时,您也在扫描它,因为您的应用已经更改了相应的文件,确切地说是创建文件。因此,该文件将立即可供其他应用程序使用。但是,由于文件位置在您的应用程序缓存目录中,因此其他应用程序无法访问它。如果您不拨打MediaScanner.scanFile,通常您必须重新启动设备以更新文件信息。如果您正在创建一个临时文件,我认为您无需致电MediaScanner.scanFile,因为您会立即将其删除。 那么删除后,还需要重新扫描文件,让其他应用程序知道文件已被删除。

另外,尽管直接使用MediaScannerConnection.scanFile,如果你支持android版本Intent.ACTION_MEDIA_MOUNTED代替广播。而且我还建议您直接广播数据,因为根据我的经验,MediaScannerConnection.scanFile 在我的一台测试设备上失败了。

Intent mediaScanIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(Uri.fromFile(new File(imagePath)));
} else {
    Uri fileUri = Uri.parse("file://" + imagePath);
    mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED, fileUri);
}
context.sendBroadcast(mediaScanIntent);

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多