简体   繁体   中英

Jetpack Compose - Resizing Image after Image PIcker (ContentResolver Exception?)

I'm just trying to resize an image after the user launches the Image Picker from my app and chooses an image file on the local device (handling a remote image from Dropbox or something will be another battle) and while this has worked for me previously, now I'm getting this exception:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1105296364, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/document/primary:Download/20170307_223207_cropped.jpg flg=0x1 }} to activity {my.app/MainActivity}: java.io.FileNotFoundException: No content provider: /document/primary:Download/20170307_223207_cropped.jpg

This occurs after the image is chosen in the Picker, because I'm running my "processing" code to locate the image, resize it, and copy it to a subfolder in the app's folder.

Like I said, this worked, but I'm not sure what's wrong now. I've tried this on the emulator as well as on my Galaxy S10 via USB debugging and it's the same result. The image is in the local storage "Download" folder on the emulator as well as my own device.

The URI looks weird (I mean the picture is just in the local storage "Download" folder) but I'm no URI expert so I assume it's fine, because that's what the Image Picker returns.

Here's the immediate code that's throwing the exception (specifically, the ImageDecoder.decodeBitmap call):

private fun copyFileToAppDataFolder(
        context: Context,
        imageTempPath: String
    ): String {
        // ensure we are sent at least a non-empty path
        if (imageTempPath.isEmpty()) {
            return ""
        }

        val appDataFolder = "${context.dataDir.absolutePath}/images/firearms"
        var filename = imageTempPath.substringAfterLast("/", "")

        if (filename.isNullOrBlank()) {
            filename = imageTempPath.substringAfterLast("%2F", "")
        }

        // couldn't parse filename from Uri; exit
        if (filename.isNullOrBlank()) {
            return ""
        }

        // get a bitmap of the selected image so it can be saved in an outputstream
        var selectedImage: Bitmap? = null
        selectedImage = if (Build.VERSION.SDK_INT <= 28) {
            MediaStore.Images.Media.getBitmap(context.contentResolver, Uri.parse(imageTempPath))
        } else {
            ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, Uri.parse(imageTempPath)))
        }

        if (selectedImage == null) {
            return ""
        }

        val destinationImagePath: String = "$appDataFolder/$filename"
        val destinationStream = FileOutputStream(destinationImagePath)
        selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, destinationStream)
        destinationStream.close()

        return destinationImagePath
    }

That above function is called from my ViewModel (that processFirearmImage function is just calling the one above), where I send the result URI from the image Picker as well as the Application Context:

// this event is fired when the Image Picker returns
is AddEditFirearmEvent.AssignedPicture -> {
                val resizedImagePath = ShotTrackerUtility.processFirearmImage(
                    event.applicationContext, // this is from LocalContext.current in Composable
                    event.value // result uri from image picker
                )

                _firearmImageUrl.value = resizedImagePath
            }

I don't know, lol. I can't believe this is such a difficult thing but information for this sure seems sparse (for Compose especially, but even so) but I don't really consider launching an Image Picker and resizing the resulting image to be that weird. Any help would be great from you smart people.

Taking a step away from programming problems and coming back seems about the best bet sometimes, lol.

I came back tonight and within a couple minutes noticed that I was sending an improper Uri to the ImageDecoder.createSource method that was causing the exception. Basically this was happening:

val imageTempPath = theUriReturnedFromImagePicker.path ?: ""
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, Uri.parse(imageTempPath)))

And it should've been:

val imageUrl = theUriReturnedFromImagePicker
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, imageUri))

As I mentioned in the OP, this originally worked but I must've changed code around a bit (arguments I'm sending to various methods/classes, mostly). I'm also using that Uri.path part to get the filename of the image chosen so I overlooked and/or got confused to what I was sending to ImageDecoder.createSource .

Doh. Maybe someone else will do something dumb like me and this can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM