Camera Intent,FileUriExposedException,在存储中保存图像并检索图像位图和文件路径

Camera Intent, FileUriExposedException, saving image in storage and retrieving the image bitmap and file path

在牛轧糖以下的 android 设备上,我可以毫无问题地将图像保存在设备的外部存储器中,然后存储文件路径并使用 Picasso 显示图像。

但是,在 Nougat 设备上进行测试时,每当我启动相机意图时,我都会收到 FileUriExposedException。

我在manifest中添加了fileprovider,还添加了一个filepaths.xml

Manifest.xml:

<provider
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true"
        android:name="android.support.v4.content.FileProvider">

        <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />

    </provider>

filepaths.xml:

<paths>
<external-path name="mediaimages" path="." />
</paths>

这是我的代码

MainActivity.java

 Intent takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                            String authorities = getPackageName() + ".fileprovider";

                            File image = getTempFile(AddListingPage2Activity.this);
                            try {
                                image.createNewFile(); //Getting ignore warning
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            Uri imageUri = FileProvider.getUriForFile(AddListingPage2Activity.this, authorities, image);
                            mImageFileUri = imageUri;

                            takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                            startActivityForResult(takePicture, CAMERA_REQ);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
           case CAMERA_REQ:
            if (resultCode == RESULT_OK) {
              //How to get bitmap and create file to store in storage
                 Bitmap bitmap = null;
                 bitmap = BitmapFactory.decodeFile(mImageFileUri.getPath()); //returns null

                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageFileUri); //returns null as well
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;

}

private File getTempFile(Context context) {
        final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
        if (!path.exists()) {
            path.mkdir(); //Getting ignored warning
        }
        return new File(path, "myImage.jpg");
    }

如何在 OnActivityResult 中保存图像文件并检索文件路径?

我弄明白为什么相机应用无法保存图像了。该应用程序应该将图像保存在 ./packagename/myImage.jpg 中,但 myImage.jpg 被创建为目录。在我删除文件夹并尝试 运行 应用程序后,我终于可以保存图像了。

你应该更新 filepaths.xml

   <paths>
    <external-path
        name="external_files"
        path="." />
</paths>

和Mainifest.xml文件

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>