如何在按钮Android Studio中添加图像背景

时间:2019-02-14 16:50:59

标签: android kotlin

我想在按钮视图中添加图像,但未显示,请帮助并纠正我。 这是我的mainActivity代码:

select_image_button.setOnClickListener {
        Log.d("MainActivity", "try to show Photo")

        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        startActivityForResult(intent,0)
    }

Override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null){
        Log.d("MainActivity", "Photo was selected")

        val uri = data.data
        val bit = MediaStore.Images.Media.getBitmap(contentResolver,uri)

        val bitmapDrawable = BitmapDrawable(bit)
        select_image_button.setBackgroundDrawable(bitmapDrawable)
    }

这是我的XML代码:

<Button
        android:textColor="@android:color/white"
        android:text="Select Photo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/select_image_button"
        />

2 个答案:

答案 0 :(得分:0)

select_image_button.background = bitmapDrawable

答案 1 :(得分:0)

编辑:澄清之后,这就是您要寻找的内容:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val intent = Intent(Intent.ACTION_GET_CONTENT)
    intent.type = "image/*"
    if (intent.resolveActivity(packageManager) != null) {
        startActivityForResult(intent, REQUEST_SELECT_IMAGE_IN_ALBUM)
    }
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode === REQUEST_SELECT_IMAGE_IN_ALBUM && resultCode === Activity.RESULT_OK) {
        val selectedImage = data?.data
        decodeUri(selectedImage!!);
    }
}

fun decodeUri(uri: Uri) {
    var parcelFD: ParcelFileDescriptor? = null
    try {
        parcelFD = contentResolver.openFileDescriptor(uri, "r")
        val imageSource = parcelFD!!.fileDescriptor

        // Decode image size
        val o = BitmapFactory.Options()
        o.inJustDecodeBounds = true
        BitmapFactory.decodeFileDescriptor(imageSource, null, o)

        // the new size we want to scale to
        val REQUIRED_SIZE = 1024

        // Find the correct scale value. It should be the power of 2.
        var width_tmp = o.outWidth
        var height_tmp = o.outHeight
        var scale = 1
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                break
            }
            width_tmp /= 2
            height_tmp /= 2
            scale *= 2
        }

        // decode with inSampleSize
        val o2 = BitmapFactory.Options()
        o2.inSampleSize = scale
        val bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2)

        imageButton.setImageBitmap(bitmap)

    } catch (e: FileNotFoundException) {
        // handle errors
    } catch (e: IOException) {
        // handle errors
    } finally {
        if (parcelFD != null)
            try {
                parcelFD.close()
            } catch (e: IOException) {
                // ignored
            }

    }
}

我也尝试过自己。

相关问题