简体   繁体   中英

File is not being deleted even though file.delete() is returning true

I have an activity which allows the user to snap a picture and onActivityResult() will create a temp file in the cache dir to store it before i upload it to the server.

This is how I start the intent:

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

Here is the code inside 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) {

        }

    }
}

And displayUserPhoto is just a simple Glide call:

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

Since I want to override the previous picture if a user decides to retake the picture, I check if the photoFile is a file. If it is, I delete it. Then create a new file.

The problem is that it always return the same initial picture. The file is never deleted even though I call .delete() .

Since I am using the app's cache dir, I don't need write permissions, but just incase I tried including that but it still didn't work.

Edit: added full flow below

I don't really know what to do here since the answer is completely different than what I initially thought it was, so it doesn't really pertain to the question.

Glide was not only saving a cache in memory but also on disk, hence why i kept getting the same image.

The solution is simply this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM