我有一个ImageView,它应该用以下代码从图库中填充一张图片:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Pick an image.");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE_REQUEST);
现在,这段代码的这部分一切正常,问题在onActivityResult()中,确切地说是在setImageBitmap()中,因为imageView没有采用来自Gallery的图像。这是来自onActivityResult()的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE_REQUEST || resultCode == PromoUpload.RESULT_OK){
Uri selectedImage = data.getData();
String[] filepathColumm = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filepathColumm, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filepathColumm[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
imagePromo.setImageBitmap(bitmap);
pictureFlag = 1;
}
我烤了一下
picturePath
属性,它正常显示了,问题就出在这里:Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
imagePromo.setImageBitmap(bitmap);
但是我不知道我做错了什么。这是ImageView的XML代码:
<ImageView
android:id="@+id/imageView_promo"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:padding="1dp" />
我也尝试从多个尺寸中选择图像,但似乎无法正常工作...