File.delete()不会完全删除留下的图像空白图像文件 [英] File.delete() does not completely delete image blank image file left behind

查看:74
本文介绍了File.delete()不会完全删除留下的图像空白图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app you it takes a picture saves it to the SD card then user gets to chose what to do with it. Delete, save, or send. If you press delete. I call File.delete() it deletes the files when I go look in the gallery and then when I sometimes go back later I see a black image saying file cannot load. That image is the one I attempted to delete earlier. What is wrong with this and why doesn't it completely go away?

我如何保存图像:

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();
    }

在onClick上删除图片的地方

where onClick where image is delete

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

推荐答案

这不是对您的特定问题的直接答案,但我想提出一个不同的工作流程,以完全避免问题.

This isn't a direct answer to your specific question, but I'd like to propose a different work flow that may avoid the problem entirely.

第一次拍摄照片时,请将其保存在内存中(使用 BitmapFactory.decodeByteArray 而不是 BitmapFactory.decodeFile ),或将文件写入临时文件(参见File.createTempFile ).无论哪种情况,其想法都是将文件写入图库目录.

When you first take the picture, either keep it in memory (use BitmapFactory.decodeByteArray instead of BitmapFactory.decodeFile), or write the file to a temp file (see File.createTempFile). In either case, the idea is to not write the file to the gallery's directory.

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

Then, if and when the user chooses 'save', write/copy the file to the to the gallery's directory. If they choose 'delete', delete the temp file (or don't, and let the OS clean it up).

写入文件(保存)后,使用

Once you write the file (save), update the gallery with the one specific file using

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

中所述的

拍照后要画廊吗?

这篇关于File.delete()不会完全删除留下的图像空白图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆