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

时间:2019-01-09 04:08:20

标签: android android-intent android-camera

我正在遵循Google教程中有关启动拍摄和传送照片的新意图。 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文件:

enter image description here

这是该文件的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>

1 个答案:

答案 0 :(得分:4)

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

您当前的提供者路径为

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

应该是

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

希望有帮助