使用相机拍照并保存在 FileProvider 中

Take photo with Camera and save in FileProvider

我需要有关使用 FileProvider 在 Android(Java)拍摄照片的帮助。 我写了关于拍照的 Android 教程(+ 更多),所以我遇到了以下情况: 我的清单:

    <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>

我的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>

那么调用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();
        }
    }

最后,onActivityResult:

if (requestCode == Util.getResources().getInteger(R.integer.ACTION_FOTO_CAPTURE)) {
    doSomething();
}

问题是,在startCamera方法中,第14行:

    Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", image);

我有一个 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)

移除
路径 = "Android/data/com.android.example/files/Pictures" 到路径 = "/"

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