admin管理员组

文章数量:1431391

In my Android App, I use ActivityResultContracts.TakePicture to store a picture in a target URI gerated from FileProvider. This is how I dispatch the TakePicture intent and open the camera:

actionTakePicture = activity.registerForActivityResult(
                new ActivityResultContracts.TakePicture(),
                success -> onPictureTaken(success));

public void dispatchTakePictureIntent(@Nullable Bundle bundle) {`  
  // Create the File where the photo should go
         File photoFile;
         try {
             photoFile = createImageFile();
         } catch (IOException ex) {
             throw new RuntimeException(ex);
         }
         this.bundle = bundle;
         Uri photoURI = FileProvider.getUriForFile(activity,
                        "at.kandu.warehouse.fileprovider", photoFile);
         actionTakePicture.launch(photoURI);
}

private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalStorageFilesDir();
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        imagePath = image.getAbsolutePath();
        return image;
    }

I have this in my AndroidManifest.xml:

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<provider
  android:name="androidx.core.content.FileProvider"
  android:authorities="at.kandu.warehouse.fileprovider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_paths" />
</provider>

And this is my file_paths file:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path
        name="pdf_cache"
        path="pdfs" />
    <files-path
        name="photos_awaiting_upload"
        path="pictures/upload" />
    <external-path
        name="my_images"
        path="Android/data/at.kandu.warehouse/files/Pictures" />
</paths>

The photo URI is correct and when opening the camera app, the target file is created and its empty. My problem appears after updating version or reinstalling my android app and then dispatching the TakePicture intent. On the first time, it asks for permissions to use the camera and the Camera app is opened as an invidual application as it can be seen in the picture:

Camera app as an individual app in the stack

If I take a picture at this moment, the picture is not stored in the target file in photoURI, but in the photo gallery of the phone and the intent doesnt returns and **onPictureTaken(success) **method is never called.

If I try to open again the TakePicture intent, the camera app looks like it is part of the application stack and correctly stores the photos in the target file in photoURI

Camera app part of my app's stack

Does anyone know how to solve this? Maybe the permissions pop up is the problem.

I made sure that the file exists in the target photoURI. The permissions to open camera are granted. This only happens the first time I dispatch the TakePicture intent after reinstalling the app.

本文标签: javaActivityResultContractsTakePicture saves picture in Gallery instead of target URIStack Overflow