Picking Image from Gallery ( Gallery Intent Way)

33 sec read

Dear Friends

Create and android project ( follow tutorial Link

Add a button in Layout XML File and write text on button it as Use Gallery like below

<Button android:id="@+id/galleryBtn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="Use Gallery"/>

 

Now open a gallery using intent

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);

 

And in onActivityResult you get the result back when you have picked image from gallery.

 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Uri photoUri = Uri.fromFile(fileCamera);
            if (photoUri != null) {
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
                    selectedImage.setBitmapC(bitmap);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

So now once you get bitmap you can show that in a imageview using

imageview.setBitmap(bitm);

 

 

Thanks for reading.

Happy Coding.