如何将图像从Gallery显示到ImageView?

时间:2015-07-04 22:40:09

标签: android android-camera android-imageview android-gallery

我想将从图库中拍摄的图像显示到imageview中。但这不会发生。我不明白为什么。我可以很好地使用相机功能,并从相机中查看图像视图中的图像,但不能从图库中查看图像。你能帮帮我吗?

这是我的代码

protected Button mFromCamera;
protected Button mFromGallery;
protected ImageView mImageView;

private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PHOTO = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Initialize ImageView
    mImageView = (ImageView) findViewById(R.id.ImgPrev);
    //Initialize Camera
    mFromCamera = (Button) findViewById(R.id.FromCamera);

    //use camera
    mFromCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        } //use camera end

    });

    //initialize button
    mFromGallery = (Button) findViewById(R.id.FromGallery);

    //pick a photo
    mFromGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });//pick a photo end
}



//previewing Image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        //from the gallery
        case SELECT_PHOTO:
            if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null!= data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
            break;
        //from the camera
        case CAMERA_REQUEST:
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                mImageView.setImageBitmap(photo);
            }
            break;
    }
}//Preview Image End

唯一的问题是,从图库中选择的图像未显示在ImageView中。我已经尝试过处理" SELECT_PHOTO"就像我使用" CAMERA_REQUEST"一样。而且它也没有成功。

1 个答案:

答案 0 :(得分:1)

也许BitmapFactory.decodeFile(picturePath)返回null。核实。并检查您是否在AndroidManifest.xml中添加了权限:

android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE

文档中的BitmapFactory.decodeFile(String filePath): http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)

<强>返回: 生成的解码位图,如果path为null或无法解码,则 null