我正在尝试从图库中选择图像,将其转换为Bitmap
,然后将其显示为ImageView
.
下面的代码用于从图库中选择图像
Intent pickPhoto = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);`
在onActivityResult
我将其转换为 aBitmap
并将其显示在ImageView
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;//returning null for below statement
bitmap = BitmapFactory.decodeFile(selectedImage.toString(), options);
productItemImage.setImageBitmap(bitmap);
}
break;
}
}
所选图像 Uri 中的值返回为“content://media/external/images/media/3647”
productItemImage
是我ImageView
显示的Bitmap
。没有错误,但位图返回 null。
请帮忙