android image picker, android image picker library, android write permissions,pick image from gallery in android, image picker

How to pick an image from gallery using image picker in android

With the use of android applications as the most preferred way by users, business owners and those offering services have had to adapt to this rapidly growing technology.

Android applications have the basic features of capturing data which are not limited to fields like user full name, email address, phone number, and password. But for others, there is always a need to have a complete user profile which includes uploading of passport image and identification document.

A Self-registration app ensures that they collect all the basic information which forms part of the KYC (know your customer) making upload of files or images an important feature.

In our previous articles, we covered how to upload a file in android whereby we covered the process of moving an image from a device to an online server using fast networking library.  

For this article, we shall discuss how to pick an image from the gallery on your device using the android picker method and make it ready for upload to an online server.

What is image picker in android?

Image picker is a process followed in android development that allows an application to access device gallery or storage files and allows choosing a file that one is interested in.

The file picked is then copied and stored in temporary memory in the application ready for either upload or storage to other locations.

Why is image picker important in android?

Image picker in android is necessary for applications that allow the custom upload of files or file submission to a different location for information processing. An example of an application that utilizes image picker is Uber driver app where a driver has to upload documents during self-application.

Image picker is also necessary in cases where custom files are needed, that is when a user needs to change a profile picture for example on Twitter and Facebook.

How to pick an image or file from a gallery in android

To allow your application to pick an image or file from a gallery or other location in a device, you will need to follow the steps that are highlighted below.

  • First, ensure you set up your application in the right way, follow this  article to create an app
  • In the manifest file, declare permissions that will allow your application to access the user device storage or gallery.

The permissions include read external storage and write external storage. Without these permissions, your application will not have access to the gallery

In your android manifest file between the <application> </application> tags, add these lines of code

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

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

  • Still in the android manifest file between the application tags as above, you will need to allow apps of different API levels to use attributes that are not found in them using the requestLegacyExternalStorage attribute as shown below

android:requestLegacyExternalStorage="true"

  • In the XML file, you will need to create an image view where the image selected from the gallery will be previewed and also a button or text view that will trigger or initialize the image picker.

The XML code for image view and button or text view is as follows

                   <RelativeLayout

                    android:id="@+id/lay_rel_gallery"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent"

                    android:layout_marginTop="5dp"

                    android:orientation="vertical">

                   

                 <ImageView

                        android:id="@+id/view_gallery"

                        android:layout_width="100dp"

                        android:layout_height="100dp"

                        android:layout_centerHorizontal="true"

                        android:layout_centerVertical="true"

                        android:layout_margin="5dp"

                        android:scaleType="fitXY" />

                    <TextView

                        android:id="@+id/text_select3"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:layout_centerHorizontal="true"

                        android:layout_centerVertical="true"

                        android:drawableStart="@drawable/add_image"

                        android:drawablePadding="5dp"

                        android:gravity="center"

                        android:text="choose image"

                        android:textSize="14sp" />                           

                </RelativeLayout>

  • In the java or kotlin file, you will need to add the code that will assist in picking image from the gallery.
  • Check whether the device trying to use the storage permissions has a greater SDK version than the one defined in the gradle file

If its not, you will need to request for permissions at the same time call the method to initiate image chooser as shown below

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);

                    imageChooser();

                } else {

                    imageChooser();

                }

  • Implement the imagechooser() method that will allow opening and selecting of an image from the gallery

                public void imageChooser() {

                    //Pick Image From Gallery

                    Intent i = new Intent(Intent.ACTION_PICK,           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                  startActivityForResult(i, SELECT_PICTURE);

                }

  • Next, add function to the activity for result that is defined above

     public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            // compare the resultCode with the

            // SELECT_PICTURE constant

            if (requestCode == SELECT_PICTURE) {

                // Get the url of the image from data

                Uri selectedImageUri = data.getData();

                if (null != selectedImageUri) {

                    // update the preview image in the layout using the picasso library

                    Picasso.get().load(selectedImageUri).into(img_gallery);

                }

            }

        }

    }

  • Once the image is displayed in the image view for preview, you will have successfully used the image picker to select an image from the device gallery
  • To upload the image to a server follow this previous article that we did on how to upload an image to an online server

Conclusion

In the above discussion, we have highlighted on the steps followed when picking an image from a gallery in android.