【问题标题】:File is not being deleted even though file.delete() is returning true即使 file.delete() 返回 true,文件也没有被删除
【发布时间】:2015-10-23 01:44:31
【问题描述】:

我有一个允许用户拍照的活动,onActivityResult() 将在缓存目录中创建一个临时文件来存储它,然后再将其上传到服务器。

这就是我开始意图的方式:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);

这是onActivityResult里面的代码:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == Activity.RESULT_OK) {

        if (requestCode == REQUEST_CODE_CAMERA) {
            try {
                Bitmap photo = (Bitmap) data.getExtras().get("data");

                File photoFile = new File(getActivity().getCacheDir(), "userprofilepic_temp.jpg");

                boolean b = false;
                if(photoFile.isFile()){
                    b = photoFile.delete();
                }
                b = photoFile.createNewFile(); //saves the file in the cache dir, TODO delete this file after account creation
                userPhotoFilePath = photoFile.getAbsolutePath();

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

                FileOutputStream fos = new FileOutputStream(photoFile);
                fos.write(bytes.toByteArray());
                fos.close();

                displayUserPhoto(photoFile);

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        else if (requestCode == REQUEST_CODE_PHOTO_LIBRARY) {

        }

    }
}

而 displayUserPhoto 只是一个简单的 Glide 调用:

@Override
public void displayUserPhoto(File photoFile) {
    Glide.with(this)
            .load(photoFile)
            .into(userPhotoView);
}

如果用户决定重新拍摄照片,我想覆盖上一张照片,因此我检查 photoFile 是否为文件。如果是,我删除它。然后创建一个新文件。

问题是它总是返回相同的初始图片。即使我调用.delete(),文件也不会被删除。

由于我使用的是应用程序的缓存目录,因此我不需要写入权限,但以防万一我尝试包含该目录但它仍然无法正常工作。

编辑:在下方添加完整流程

【问题讨论】:

  • 您是否尝试过在调试器中单步执行此操作,在delete() 之后中断,并在创建和编写新文件之前观察文件是否实际被删除?如果是,那么您看到的行为可能是由将相同的数据重新写入新文件引起的。
  • "即使我调用了 .delete(),文件也永远不会被删除。" -- 即使文件没有被删除,你也会覆盖它的内容。因此,您的问题出在其他地方,可能在您的 displayUserPhoto() 实现中。另外,请去掉ByteArrayOutputStream,因为这会严重浪费内存,因为您只是转身使用FileOutputStream 写入数据。将FileOutputStream 传递给compress()
  • @CommonsWare 我添加了完整的流程。我看不出它可能在哪里搞砸了。流程似乎很基本。
  • 我猜 Glide 并没有检测到图像文件改变了内容并且完全在内存缓存中工作的事实。
  • @AndyThomas 我做了,因为 delete() 返回 true,所以它被删除了。同样 createNewFile 也返回 true(如果它没有创建新文件,它将返回 false)。

标签: java android android-file


【解决方案1】:

我真的不知道该怎么做,因为答案与我最初认为的完全不同,所以它与问题无关。

Glide 不仅将缓存保存在内存中,还保存在磁盘上,这就是为什么我一直得到相同的图像。

解决方法很简单:

Glide.with(this)
            .load(photoFile)
            .skipMemoryCache(true)//this
            .diskCacheStrategy(DiskCacheStrategy.NONE)//and this
            .into(userPhotoView);

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    相关资源
    最近更新 更多