Search code examples
androiddelete-fileandroid-camera2save-image

Android Camera2 API - Overwrite old images


I am using the code given at https://github.com/googlesamples/android-Camera2Basic and images are being saved in the activity storage with

    @Override
    public void run() {
        ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(mFile, false);
            output.write(bytes);
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            mImage.close();
            if (null != output) {
                try {
                    output.close();
                    buffer.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

given by the sample code. If I delete the images with

   File image_directory = getFilesDir();
    if (image_directory != null) {
        File[] images_list = image_directory.listFiles();
        for (int i = 0; i < images_list.length; i++) {
            try {
                //Delete the images files in the given folder
                images_list[i].delete();
                Log.e(TAG, "[-] File deleted with filename: " + images_list[i].getName());
            } catch (Exception e) {
            }
        }
    }

and take new images with the same filename as before, the old images show up. What`s happening here?


Solution

  • Ok, code is correct. I am using Glide (https://github.com/bumptech/glide) to display the images and the lib cached the images based on the image names. While the new images had been stored on the device, the old, cached images have been displayed.