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:
- Capture image from camera
- Choose image from gallery
For getting images, you need to follow these steps
- Check if the device has a camera, if not you can pick an image from the gallery.
- Check the permission for accessing external storage.
- 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 | |
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 ImageUtils.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 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"/> |
Jaison Fernando
Latest posts by Jaison Fernando (see all)
- Phone number auth using Firebase Authentication SDK - March 20, 2020
- Password-less email auth using Firebase Authentication SDK - March 9, 2020
- How to use SharedPreferences API in Android? - February 10, 2020