使用相机捕获的Android图像不会保存在Android牛轧糖上的指定自定义文件夹中



我试图使用Android Camera将图像保存在名为" AppFolder"的文件夹中。我的目标SDK为25。我的设备在Android Nougat上运行。但是,当我使用" dispatchTakePictureIntent()"单击图像时。该图像无法保存在AppFolder中。它保存在DCIM/Camera文件夹中。为什么会发生这种情况以及如何将其保存在我的自定义文件夹中?

 private void dispatchTakePictureIntent() {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // Ensure that there's a camera activity to handle the intent
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        // Error occurred while creating the File
                        Log.i("imageCaptutreError", ex.getMessage());
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Uri photoURI = FileProvider.getUriForFile(this,
                                "com.abc.def",
                                photoFile);
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                    }
                }
            }
    private File createImageFile() throws IOException {
            File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder");
            if (!folder.exists()) {
                folder.mkdir();
            }
            File tempFile = new File(folder, "temp_image.png");
                    /*new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "appFolder" + File.separator + "temp_image.png");*/
            mCurrentPhotoPath = tempFile.getAbsolutePath();
            return tempFile;
        }

mainifest中的提供商

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

@xml/file_paths

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

部分是因为您没有在Intent上调用addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)。就目前而言,另一个应用程序没有对Uri标识的位置的写入访问权限。

但是,请记住,第三方相机应用程序有错误。理想情况下,他们尊重EXTRA_OUTPUT。但是,有些不会:

  • ...因为他们一般忽略了EXTRA_OUTPUT
  • ...因为他们不知道如何处理EXTRA_OUTPUTUri上的content方案(即使Google自己的相机应用也有此问题,直到2016年中)

fwiw,此示例应用程序使用ACTION_IMAGE_CAPTUREFileProvider一起显示。

最新更新