1 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是一個(gè)空指針異常。imageUri 為空
MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
如果您檢查 getBitmap() 的實(shí)現(xiàn)。
看
public static final Bitmap getBitmap(ContentResolver cr, Uri url)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
}
你會(huì)發(fā)現(xiàn)它調(diào)用了 openInputStream() ,它需要一個(gè)非空的 uri。
public final @Nullable InputStream openInputStream(@NonNull Uri uri)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (this.imageUri != null && requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {
try {
//Getting the Bitmap from Gallery
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
this.imgThumb!!.setImageBitmap(bitmap)
this.pictureTaken = true
} catch (e:IOException) {
e.printStackTrace()
}
} else {
Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
}
}
添加回答
舉報(bào)