Search code examples
androidandroid-cameraandroid-imageviewandroid-gallery

How to show image from Gallery to ImageView?


I want to show the image taken from the gallery into the imageview. But this is not happening. I don't understand why. I can use the camera feature just fine and see the image in the imageview from the camera, but not the one taken from the gallery. Can you guys help me out, please?

Here is my code

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

The only problem is, the image selected from the gallery is not shown in the ImageView. I've tried treating the case for the "SELECT_PHOTO" the same way as what I've done with "CAMERA_REQUEST". and it didn't work out as well.


Solution

  • Maybe BitmapFactory.decodeFile(picturePath) returns null. Check it. And check if you added permissions in AndroidManifest.xml:

    android.permission.WRITE_EXTERNAL_STORAGE
    android.permission.READ_EXTERNAL_STORAGE
    

    BitmapFactory.decodeFile(String filePath) in documentation: http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)

    Returns: the resulting decoded bitmap, or null if path is null or could not be decoded.