0

I am using fileprovider to save a photo to my device on Android 13 with the camera intent. This only seems to work when the folder or file being saved to doesnt exist.

Bear in mind the device does not have external card slot, its all internal memory, so although we set the read and write permissions in the manifest, we only request for camera and read images permission during runtime.

So we set the path as: /storage/emulated/0/DCIM/NewCam/NewCamPhoto.jpg

If I manually delete the existing photo the camera works fine and saves the new photo it takes. So I thought before we start the camera intent, lets check and delete any existing photo at location we specify. I tried to use the code below:

public static void deleteFolder(String folderPath) {
    Log.d("yoyo","**** SCANNER **** DELETING "+folderPath);
        File folder = new File(folderPath);
        if (folder.exists()) {
            String[] children = folder.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteRecursive(new File(folder, children[i]));
                if (!success) {
                    return;
                }
            }
            // The folder is now empty so delete it
            folder.delete();
        }
    }

    private static boolean deleteRecursive(File file) {
        if (file.isDirectory()) {
            String[] children = file.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteRecursive(new File(file, children[i]));
                if (!success) {
                    return false;
                }
            }
        } else {
            return file.delete();
        }
        return true;
    }

But it does not delete anything, the photo still stays there. So how would I delete that photo if it exists?

The camera code is:

            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photo = new File(path, Name); if (photo.exists()) photo.delete();
            PhotoPath = photo.getAbsolutePath();
            i.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(RunnerActivity.CurrentActivity, RunnerActivity.CurrentActivity.getPackageName() + ".NewCam",photo));
            RunnerActivity.CurrentActivity.startActivityForResult(i, REQUEST_CODE_CAMERA);

As mentioned in the details I did try to delete the photo or the folder before we start the new camera intent. It seems that there is something stopping the deletion of the existing photo. Maybe because its in the DCIM folder but the actual photo is in a subfolder in DCIM.

This is the FileProvider I have setup in the manifest:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.cam.newcam.scannerprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

and in res/xml I have provider_paths.xml as:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />

    <!-- Access to a specific subdirectory within DCIM -->
    <external-path name="external_dcim" path="DCIM/NewCam" />
</paths>
rafhelp
  • 13
  • 4

0 Answers0