【问题标题】:imageUri retrieved from FileProvider.getUriForFile() is set on imageView but shows empty ImageView从 FileProvider.getUriForFile() 检索的 imageUri 在 imageView 上设置,但显示为空 ImageView
【发布时间】: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


【解决方案1】:

清单文件

<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);

onActivityResult

if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
    {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  

【讨论】:

  • 这是你的工作代码吗?
  • 我的错误,但我需要得到imageUri 才能写信给FirebaseStorage。如果无法访问imageUri,我该怎么办?
  • 检查此链接以将您的 uri 上传到 firebaseStorage 中:firebase.google.com/docs/storage/android/upload-files
【解决方案2】:

在这篇帖子How to get Image URI by intent MediaStore.ACTION_IMAGE_CAPTURE in Android 10 and above 中找到了我的解决方案。按照它的方式,可以检索 URI 并将其设置为 imageView。其他解决方案在 kotlin 中要么过时,要么没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多