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
版权声明:本文标题:java - ActivityResultContracts.TakePicture saves picture in Gallery instead of target URI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745572470a2664128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论