首页 > 解决方案 > 使用 FileProvider 和 ActivityResultContracts.TakePicture() 的问题

问题描述

您好我正在尝试使用新的AndroidX Activity Result APIs,尤其是ActivityResultContracts.TakePicture()合同。在放入我的真实应用程序之前,我创建了一个示例项目进行测试。

主要活动

class MainActivity : AppCompatActivity() {

    private var _binding: ActivityMainBinding?=null
    private val binding get() =_binding!!

    private var photoUri:Uri?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityMainBinding.inflate(layoutInflater)
        setupButtons()
        setContentView(binding.root)
    }

    private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSaved ->
        if (isSaved) {
            binding.imageView.setImageURI(photoUri)
        }
    }

    private fun setupButtons() {
        binding.button.setOnClickListener {
            photoUri = pickUri()
            takePicture.launch(photoUri)
        }
    }

    private fun pickUri(): Uri {
        val path = applicationContext.getExternalFilesDir(null)?.absolutePath
        val picturesFolder = File(path, "pictures")

        Log.d(TAG, "pickUri: ${picturesFolder.absolutePath}")
        val sdf = SimpleDateFormat("yyyyMMdd_HHmmss")
        val dt = sdf.format(Date())
        val fileName = "profile-picture-user-4-${dt}.jpg"
        val imagePath = File(applicationContext.filesDir, "pictures")
        val newFile = File(imagePath, fileName)
        if (!picturesFolder.exists()){
            picturesFolder.mkdirs()
            Log.d(TAG, "pickUri: directoryCreated")
        }

    return getUriForFile(applicationContext, "com.example.resultactivitytutorial.fileprovider", newFile)
}

我想要实现的是:如果未创建,则创建一个名为picturesinside的文件夹以PACKAGE_NAME-> files保存个人资料图片,并使用新的 ActivityResultApi 将拍摄的照片保存在此文件夹中。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.resultactivitytutorial">

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.ResultActivityTutorial">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.resultactivitytutorial.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>

</manifest>

@xml/file_paths文件是:

<paths>
<files-path
    name="pictures"
    path="pictures/" />
</paths>

问题是我收到错误

W/ImageView: Unable to open content: content://com.example.resultactivitytutorial.fileprovider/pictures/profile-picture-user-4-20210118_132245.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)

我需要一些帮助来解决这个问题。提前致谢!!

标签: androidkotlinfilenotfoundexceptionandroid-fileproviderregisterforactivityresult

解决方案


推荐阅读