拍照并保存到图库

问题描述

我已经花了几个小时研究这个问题,但没有找到解决方案(在StackOverflow上还是其他方法上)。 我的应用程序以API 30为目标,而minSskVersion为29。我正在学习本教程:https://developer.android.com/training/camera/photobasics

我的活动上有一个按钮,可以使用意图打开相机:

fun takePhoto() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        takePictureIntent.resolveActivity(packageManager)?.also {
              startActivityForResult(takePictureIntent,CAMERA_REQUEST)
        }
    }
}

但是我也想将图片保存在设备库中。因此,我将takePhoto()方法更改为:

fun takePhoto() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        takePictureIntent.resolveActivity(packageManager)?.also {
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                ex.printstacktrace()
                null
            }

            photoFile?.also {
                try {
                    val photoURI: Uri = FileProvider.getUriForFile(
                        this,"net.filiperamos.photogrid.provider",it
                    )

                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI)
                } catch (ex: IllegalArgumentException) {
                    Log.d(TAG,"Could not get file URI.")
                    ex.printstacktrace()
                }

                startActivityForResult(takePictureIntent,CAMERA_REQUEST)
            }
        }
    }
}

添加createImageFile()方法

private fun createImageFile(): File {
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(Date())
    val filename = "image_$timeStamp"
    val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)

    return File.createTempFile(filename,".jpg",storageDir).apply {
        currentPhotoPath = absolutePath // Save path for later use
    }
}

我的清单具有摄影机使用权限和外部写入权限:

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

添加一个独立于应用程序标签的提供者:

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

我创建了file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="my_images"
        path="Android/data/net.ramos.photos/files/Pictures/" />
</paths>

在设备上运行此代码时,运行FileProvider.getUriForFile()时出现异常:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/net.ramos.photos/files/Pictures/image_20200823_1415398042919497737944663.jpg

如果我更改file_paths.xml文件,将external-files-path替换为external-path错误消失了,但是没有图像存储在任何地方。

我想念什么?

解决方法

在清单文件中添加以下行作为'application'标签的属性,

android:requestLegacyExternalStorage="true"

这应该可以解决您面临的问题,并且异常应该消失。