【发布时间】:2021-12-30 13:27:00
【问题描述】:
我正在尝试显示从相机Intent 接收到的ImageView 中的图像,但 ImageView 显示的是空图像。
显示错误提示 ImageView 无法打开内容,并且无法创建图像解码器并显示消息“未实现”
W/ImageView: Unable to open content: content://com.example.mobilee_commerceapp/my_images/JPEG_20211230_214226_1746483040936834961.jpg
android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
下面是代码:
private fun dispatchTakePictureIntent() {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
null
}
photoFile?.also {
Log.d(TAG, "Success creating file")
Log.d(TAG, "PATH is " + it.absolutePath)
cameraPhotoURI = FileProvider.getUriForFile(
requireContext(),
"com.example.mobilee_commerceapp",
it
)
}
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
getCameraResult.launch(takePictureIntent)
}
getCameraResult()。 cameraPhotoURI 是全局变量。
private val getCameraResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
binding.profilePicIv.setImageURI(cameraPhotoURI)
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobilee_commerceapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"
android:required="false"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.mobilee_commerceapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
【问题讨论】:
-
我需要 imageUri 来将 Uri 保存到 FirebaseStorage,有什么办法吗?
标签: android kotlin android-intent