android - 使用 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)
}
我想要实现的是:如果未创建,则创建一个名为pictures
inside的文件夹以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)
我需要一些帮助来解决这个问题。提前致谢!!
解决方案
推荐阅读
- msp430 - 默认禁用openmsp430中断
- visual-studio - 使用 Visual Studio Code 远程启动 Singularity 容器
- python-3.x - 文件 /Users/gb/myapp/dist/MyApplication.app 的 LSOpenURLsWithRole() 失败,错误为 -10810
- spring-boot - 在 PCF 中自动缩放应用程序时 Kafka 回复超时
- arrays - Z3 Forall 带阵列
- assembly - 变量或标签如何等于数字?
- javascript - 以 4 的倍数返回值
- node.js - 如何使用 ExpressJS 在我的应用程序中正确记录错误
- ruby-on-rails - 无法加载此类文件--restclient/instrumentation
- detox - 排毒:每次测试前需要调用 device.reloadReactNative 吗?