我試圖顯示ImageView從 Camera 接收到的影像Intent,但 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>
uj5u.com熱心網友回復:
清單檔案
<manifest package="your.package.name">
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
為 android 10 添加:
android:requestLegacyExternalStorage="true"
按鈕點擊代碼
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
活動結果
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
uj5u.com熱心網友回復:
找到我的解決方案是這篇文章如何在 Android 10 及更高版本中通過 Intent MediaStore.ACTION_IMAGE_CAPTURE 獲取影像 URI。按照它的方式,可以檢索 URI 并將其設定為 imageView。其他解決方案在 kotlin 中要么已經過時,要么不在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/398994.html
