簡體   English   中英

Kotlin 選取的圖像未在對話框片段中顯示

[英]Kotlin picked image it doesn't show in Dialog Fragment

對話框片段不顯示我從圖庫中選擇的照片

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK && requestCode ==IMAGE_PICK_CODE){
        image_view.setImageURI(data?.data)
    }
}

嘗試將圖像轉換為Bitmap ,然后使用.setImageBitmap()將圖像設置為ImageView

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

    val imageUri = data?.data
    val inputStream: InputStream? 
    try {
        inputStream = imageUri?.let { activity?.contentResolver.openInputStream(it) }
        // get a bitmap from a stream
        val image = BitmapFactory.decodeStream(inputStream)
        image_view.setImageBitmap(image)
    } catch (e: FileNotFoundException) {
        Log.d("TAG", "File not found: " + e.message)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM