简体   繁体   中英

Unable to set bitmap from ActivityResult in ImageView [Kotlin]

In my app the user by clicking on a custom button is able to choose from a modal to load a picture from the gallery or take a picture, once the user done i get the image in my onActivityResult and i would set that picture as a preview instead of the "no image" i set by default.

The issue is that by doing .setImageBitmap(image) it set a gray square instead of the taken picture..

My code looks like this:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 1 || requestCode == 2) {
        val image = data?.extras!!["data"] as Bitmap?
        findViewById<ImageView>(R.id.imageView).setImageBitmap(image)
    }
}

XML

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:adjustViewBounds="true"
            android:layout_height="80dp"
            android:layout_gravity="center"
            app:srcCompat="@drawable/ic_baseline_image"
            app:tint= "#909090"
            android:contentDescription="@string/foto" />

And here is what i get:

在此处输入图像描述

The code where i create the dialog to get the image is the following:

fun takePicture(view: View) {
    val items = arrayOf<CharSequence>("Scatta la foto", "Scegli dalla Galleria", "Annulla")
    val builder = AlertDialog.Builder(this)
    builder.setTitle("Aggiungi la foto")
    builder.setItems(items) { dialog, item ->
        when {
            items[item] == "Scatta la foto" -> {
                val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(intent, 1)
            }
            items[item] == "cegli dalla Galleria" -> {
                val intent = Intent(
                    Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                )
                startActivityForResult(intent, 2)
            }
            items[item] == "Annulla" -> {
                dialog.dismiss()
            }
        }
    }
    builder.show()
}

LOL

The issue was the tint set to grey in ImageView which was showing the icon in the right way but when the picture was taken it was setting the whole picture to the greyish color

So by removing

   app:tint= "#909090"

Solved the issue.

Well in data you are not getting bitmap you are doing it wrong don't expect data to return a bitmap it will return you a URI of that picture you selected you need to get the absolute path from that photo and then load the image from that given absolute path filePath = filesManager.getPath(data?.data.,) bindImage(ui,ivProfileImage. filePath, Constants.USER_PLACE_HOLDER)

Here FileManager class is taking data from onActivitResult and converting it into absolute path afterwards loading image. You can pass that image to glide and load image into your imageview using glide.

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