Example App to show how to pick an image from Camera/Gallery
ImageUtils: Used to capture/pick the image. from camera/gallery.
It contains the following methods.
- imagepicker : shows the picker with the options Camera and Gallery.
- launchCamera : Launch camera using native intent.
- launchGallery : Launch gallery using native intent.
- checkimage : Pass the filename and the file path ,then it will return the boolean value( Image exist or not).
- getImage : Pass the filename and the file path ,then it will return the bitmap if the image not exist then return null value.
- createImage : Pass the bitmap, filename, file path, and replace flag then it will store that image in the given path in the given name.
ImageAttachmentListener: This is a callback interface which returns the image capture/pick by you.
public interface ImageAttachmentListener { public void image_attachment(int from, String filename, Bitmap file); }
For implementing this image attachment process , first you need to create an instance for the class ImageUtils.
Imageutils imageutils; imageutils =new Imageutils(this,this);
Then call the relevant function based on your need.The following steps helps you to get the image.
// Request code - 1 if (imageutils.isDeviceSupportCamera()) imageutils.imagepicker(1);
Next redirect your onRequestPermissionsResult to imageutils.request_permission_result and onActivityResult to imageutils.onActivityResult.
For getting the callbacks you need to include ImageAttachmentListener interface into your activity, and also implement the needed methods.
public class ImageAttachmentActivity extends AppCompatActivity implements ImageAttachmentListener // Callback function @Override public void image_attachment(int from, String filename, Bitmap file) { this.bitmap=file; this.file_name=filename; iv_attachment.setImageBitmap(file); String path = Environment.getExternalStorageDirectory() + File.separator + "ImageAttach" + File.separator; //Store the selected image into your desired location imageutils.createImage(file,filename,path,false); }
In AndroidManifest.xml
Specify the needed permission. If you don't, permission requests fail silently. That's an Android thing.
For more information, check out my detailed guide here : http://droidmentor.com/pick-image-from-gallery-or-camera/