gpt4 book ai didi

java - file.delete() 不彻底删除遗留的图像空白图像文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:01:08 26 4
gpt4 key购买 nike

在我的应用程序中,您可以拍照并将其保存到 SD 卡中,然后用户可以选择如何处理它。删除、保存或发送。如果按删除。我调用 File.delete() 它会在我查看图库时删除文件,然后当我有时稍后返回时,我看到一个黑色图像说文件无法加载。该图像是我之前试图删除的图像。这有什么问题,为什么它没有完全消失?

我如何保存图片:

public static File getOutputMediaFile(byte[] data){
image= BitmapFactory.decodeByteArray(data, 0, data.length);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FrontFlash");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()) return null;
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
@Override
public void onPictureTaken(byte[] data, Camera camera){
pictureFile = Util.getOutputMediaFile(data);
if (pictureFile == null){
Toast.makeText(this, "Couldn't create file", Toast.LENGTH_SHORT).show();
}
else{
try{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
catch (FileNotFoundException e){
Toast.makeText(this, "File not found exception", Toast.LENGTH_SHORT).show();
}
catch (IOException e){
Toast.makeText(this, "IO Exception", Toast.LENGTH_SHORT).show();
}
}

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FrontCam"))));
String photopath = pictureFile.getPath().toString();

//旋转图片

bmp = BitmapFactory.decodeFile(photopath);
matrix = new Matrix();
matrix.postRotate(270);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(pictureFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

where onClick where image is delete

public void exit( View view){
deleted = pictureFile.delete();
//startActivity(home);
close();
finish();
}

最佳答案

这不是对您的具体问题的直接回答,但我想提出一个可能完全避免该问题的不同工作流程。

当您第一次拍照时,要么将其保存在内存中(使用 BitmapFactory.decodeByteArray 而不是 BitmapFactory.decodeFile),要么将文件写入临时文件 (见 File.createTempFile )。无论哪种情况,我们的想法都是将文件写入画廊的目录。

然后,如果用户选择“保存”,则将文件写入/复制到图库的目录中。如果他们选择“删除”,请删除临时文件(或者不删除,让操作系统清理它)。

写入文件(保存)后,使用一个特定文件更新图库

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));

How can I update the Android Gallery after a photo? 中所述

关于java - file.delete() 不彻底删除遗留的图像空白图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com