Select Page

An image is a representation of visual information such as drawings, pictures, graphs, logos, or individual video frames.

Nowadays image sharing is an everyday routine. To share images with others, from an android app, user has two choices:

  1. Capture image from camera
  2. Choose image from gallery

For getting images, you need to follow these steps

  1. Check if the device has a camera, if not you can pick an image from the gallery.
  2.  Check the permission for accessing external storage.
  3. From camera take the picture/From gallery pick the image.

How to use

  • ImageUtils: Used to capture/pick the image. from camera/gallery.
  • ImageAttachmentListener: This is a callback interface which returns the image capture/pick by you.
ImageUtils : 

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 does not exist then return the 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 :

In this callback, it gives the bitmap file, the name of the selected file and also the request code.

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);

Then call the relevant function based on your need.The following steps help you to get the image.

// Request code – 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 ImageUtils.ImageAttachmentListener
// Callback function
public void image_attachment(int from, String filename, Bitmap file) {
String path = Environment.getExternalStorageDirectory() + File.separator + "ImageAttach" + File.separator;
//Store the selected image into your desired location

In AndroidManifest.xml

Specify the required permission. If you don’t, permission requests will fail eventually. That’s how things work in Android unfortunately .

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

View on GithubDownload .zip




Jaison Fernando

Android Developer at NFN Labs