简体   繁体   English

使用相机捕获的Android图像未保存在android Nougat上的指定自定义文件夹中

[英]Android image captured using camera doesn't get saved in specified custom folder on android nougat

I am trying to save an image in a folder named "appFolder" using android camera.My target sdk is 25.My device is running on android nougat. 我正在尝试使用Android相机将图像保存在名为“ appFolder”的文件夹中。我的目标sdk是25。我的设备正在android牛轧糖上运行。 However,when i click the image using "dispatchTakePictureIntent()". 但是,当我使用“ dispatchTakePictureIntent()”单击图像时。 The image doesn't get saved in appFolder.It gets saved in DCIM/camera folder. 图像不会保存在appFolder中,而是保存在DCIM / camera文件夹中。 Why is this happening and how to save this in my custom folder? 为什么会发生这种情况,以及如何将其保存到我的自定义文件夹中?

 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;
        }

Provider in 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 / 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>

Partly, it is because you did not call addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) on the Intent . 部分原因是您没有在Intent上调用addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) As it stands, the other app does not have write access to the location identified by your Uri . 就目前而言,其他应用程序无权访问Uri标识的位置。

However, do bear in mind that third-party camera apps have bugs. 但是,请记住,第三方相机应用程序存在错误。 Ideally, they honor EXTRA_OUTPUT . 理想情况下,他们尊重EXTRA_OUTPUT Some, however, will not: 但是,有些人不会:

  • ...because they ignore EXTRA_OUTPUT in general, or ...因为他们通常会忽略EXTRA_OUTPUT ,或者
  • ...because they do not know how to deal with the content scheme on the Uri in EXTRA_OUTPUT (even Google's own camera app had this problem until mid-2016) ...因为他们不知道如何在EXTRA_OUTPUT处理Uri上的content方案(即使Google自己的相机应用在2016年中之前也遇到了此问题)

FWIW, this sample app shows using ACTION_IMAGE_CAPTURE together with FileProvider . FWIW, 此示例应用程序显示了将ACTION_IMAGE_CAPTUREFileProvider一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM