I need help on taking a Photo in Android (in Java) using a FileProvider. I wrote the Android tutorial (+ much more) on taking photos, so I ended up with the following situation: My manifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
My res/xml/file_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="schede_images"
path="Android/data/com.android.example/files/Pictures" />
</paths>
Then, the method for callying the Camera Intent:
String currentPhotoPath;
@OnClick(R.id.startCamera)
public void startCamera(View v) {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "provaprovaprova");
if (!imagesFolder.exists()) {
boolean isCreated = imagesFolder.mkdirs();
if (!isCreated) {
Toast.makeText(this, "Errore storage", Toast.LENGTH_LONG).show();
return;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", image);
currentPhotoPath = image.getAbsolutePath();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, Util.getResources().getInteger(R.integer.ACTION_FOTO_CAPTURE));
} catch (IOException e) {
e.printStackTrace();
}
}
Finally, the onActivityResult:
if (requestCode == Util.getResources().getInteger(R.integer.ACTION_FOTO_CAPTURE)) {
doSomething();
}
The problem is that, in the method startCamera, on the 14th line:
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", image);
I have an IllegalArgumentException:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.example, PID: 25184
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/sdcard/Android/data/com.android.example/files/Pictures/IMG_20191113_152356_376811633.jpg
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)