Home > Net >  imageUri retrieved from FileProvider.getUriForFile() is set on imageView but shows empty ImageView
imageUri retrieved from FileProvider.getUriForFile() is set on imageView but shows empty ImageView

Time:12-31

I'm trying to display the image in ImageView received from Camera Intent, but the ImageView displays an empty image.

It shows error saying ImageView unable to open content, and failed to create image decoder with message 'unimplemented'

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.

Below is the code:

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 is global variable.

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>

CodePudding user response:

Manifest file

<manifest package="your.package.name">
<queries>
<intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>

add for android 10 :

android:requestLegacyExternalStorage="true"

button click code

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

CodePudding user response:

Find my solution is this post How to get Image URI by intent MediaStore.ACTION_IMAGE_CAPTURE in Android 10 and above. Followed its way and URI can be retrieved and set to imageView. Other solution is either outdated or not in kotlin.

  • Related