java - ActivityResultContracts.TakePicture saves picture in Gallery instead of target URI - Stack Overflow

In my Android App, I use ActivityResultContracts.TakePicture to store a picture in a target URI gerated

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745572470a4633762.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信