File.delete()函数在android 4.2.2版本

时间:2018-05-02 06:58:03

标签: android android-camera

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

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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