首页 > 解决方案 > File.delete() 函数在 android 4.2.2 版本中不起作用

问题描述

我正在尝试使用以下代码删除相机捕获的图像,但图像没有删除我已经尝试了很多但仍然没有结果有人可以帮助我吗

代码:

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(cameraIntent, Constants.CAMERA_REQUEST_CODE);

    private void onCaptureImageResult(Intent data) {
    File file = saveImage(this, data);
        if(file !=null){
         file .getCanonicalFile().delete();                
     }
  }


 public File saveImage(Context context, Intent data) {

        File mediaFile = null;
        try {
            Bitmap imgBitmap = (Bitmap) data.getExtras().get("data");
            File sd = Environment.getExternalStorageDirectory();
            File imageFolder = new File(sd.getAbsolutePath() + File.separator +
                    "FOSImages");
            if (!imageFolder.isDirectory()) {
                imageFolder.mkdirs();
            }
            mediaFile = new File(imageFolder + File.separator + "fos_" +
                    System.currentTimeMillis() + ".jpg");
            FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
            imgBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
            fileOutputStream.close();
            return mediaFile;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return mediaFile;
    }

标签: androidandroid-camera

解决方案


尝试这个 :

File file = saveImage(this, data);
file.delete();
if(file.exists()){
      file.getCanonicalFile().delete();
      if(file.exists()){
           getApplicationContext().deleteFile(file.getName());
      }
}

推荐阅读