在 Android 上从相机意图中获取图像

Obtaining image from camera intent on Android

我正在按照 Google 教程启动一个新的 intent 来拍摄和传送照片。 https://developer.android.com/training/camera/photobasics。我无法通过某个地点,因为我遇到了这个异常:Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.matthew.doodlewits/files/Pictures/JPEG_20190108_220143_4398098916939163453.jpg

异常发生在

Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", image);

在下面的方法中:

private void takePictureSetup()
    {
        try
        {
            Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);

            String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timestamp + "_";
            File storageDirectory = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

            File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);

            currentPathToPictureTaken = image.getAbsolutePath();

            Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", image);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, this.RESULT_CAMERA_PHOTO);

        }
        catch(Exception e)
        {
            Log.d("**EXCEPTION**", e.getMessage());
        }

    }

这是我创建的 XML 文件:

这是该文件的 xml 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

最后在我的清单文件中:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">



        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>


    </application>

问题出在您的提供商路径上

您当前的提供商路径是

Android/data/com.example.package.name/...

应该是

Android/data/com.example.matthew.doodlewits/...

希望对您有所帮助