取消照片时创建的幻影文件无法删除

when cancel photo a ghost file is created cannot delete

我创建了一个可以让你用特定名称保存照片的应用程序,过程是:1-你写名字 2-你写操作类型 3-你按下拍照 4-所有信息在 1&2 中放入名称,它会带您到相机。

拍照时一切正常,但如果您关闭相机而不是拍照,则会创建一个大小为 0 字节的幻影文件,当您使用图库打开它时,它会显示文件不存在。

 int imgNbr = 0;
values = new ContentValues();
do{
    file = new File(Environment.getExternalStorageDirectory() + "/" + containerId.getText().toString() + "_" + dateInfo + "_" + eventType + "_" + imgNbr + ".png");
    Log.d("TAG", Integer.toString(imgNbr));
    values.put(MediaStore.Images.Media.DATA, Environment.getExternalStorageDirectory() + "/" + containerId.getText().toString() + "_" + dateInfo + "_" + eventType + "_" + imgNbr + ".png");
    values.put(MediaStore.Images.Media.TITLE, containerId.getText().toString() + "_" + dateInfo + "_" + eventType + "_" + imgNbr+ ".png");
    imgNbr++;
}
while(file.exists());

Log.d("TAG", "OUT OF WHILE WITH VALUE" + Integer.toString(imgNbr-1));

imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, PICTURE_RESULT);

我试图在 activity 结果上手动删除它,但它不起作用,所有日志 return false

@Override @Nullable
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == PICTURE_RESULT){
        Toast.makeText(getApplicationContext(),"Imagen guardada correctamente",Toast.LENGTH_SHORT).show();
    }
    else if (resultCode == RESULT_CANCELED && requestCode == PICTURE_RESULT){
        //file.delete();

        Log.d("TAG", "file.isFile()" + file.isFile());
        Log.d("TAG", "file.canRead()" + file.canRead());
        Log.d("TAG", "file.delete()" + file.delete());}}

如果图片被拒绝,你应该删除文件,像这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     super.onActivityResult(requestCode, resultCode, data);

     /**** TAKING PICTURE USING CAMERA****/
     if(requestCode == PICTURE_RESULT && resultCode == RESULT_CANCELED) {
        if(imageUri!=null){ // GLOBAL variable in your activity
           File f = new File(imageUri.getPath());
           f.delete();
        }
     }
     else if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
           // do your work
     }
}

我一直在研究代码,这种方法工作正常,取消时不会创建重影。

            int imgNbr = 0;
            do{
                file = new File(Environment.getExternalStorageDirectory() + "/fotosHH/" + containerId.getText().toString() + "_" + dateInfo + "_" + eventType + "_" + imgNbr + ".png");
                imgNbr++;
            }
            while(file.exists());

            String imageFolderPath = Environment.getExternalStorageDirectory() + "/fotosHH";
            imgNbr--;
            String imageName = containerId.getText().toString() + "_" + dateInfo + "_" + eventType + "_" + imgNbr + ".png";
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imageFolderPath, imageName)));
            startActivityForResult(intent, PICTURE_RESULT);