【问题标题】:Camera Intent, FileUriExposedException, saving image in storage and retrieving the image bitmap and file pathCamera Intent,FileUriExposedException,将图像保存在存储中并检索图像位图和文件路径
【发布时间】:2018-04-24 17:18:14
【问题描述】:

在Nougat以下的Android设备上,我可以将图像保存在设备的外部存储中,然后存储文件路径并使用Picasso显示图像。

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

我在清单中添加了 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>

文件路径.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中检索文件路径?

【问题讨论】:

  • How do I save the image file ???相机应用程序会将图像保存到文件中。这就是您使用 EXTRA_OUTPUT 的原因。
  • 进一步你已经知道你确定的文件路径。
  • path.mkdir(); //Getting ignored warning 不要忽略返回值。但相应地处理。
  • 但是当我运行这个bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageFileUri);它返回null

标签: android android-camera-intent android-7.0-nougat


【解决方案1】:

我找到了相机应用无法保存图像的原因。该应用程序应该将图像保存在 ./packagename/myImage.jpg 中,但 myImage.jpg 被创建为一个目录。在我删除文件夹并尝试运行应用程序后,我终于能够保存图像。

【讨论】:

    【解决方案2】:

    你应该更新 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>
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2013-02-07
      • 2017-04-05
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多