Android: Get an image from device


There are various situations in which you have to give user a facility to select an image from their android devices. For this giving user a File explorer could be a wrong thing, you have to open the gallery so that the user can select an image. Here is the code for this scenario.

Firstly you have to create a button on whose click we will open devices library. But before that you should have a READ_EXTERNAL_STORAGE permission.

Also please remember that from API 23 and above there are Run-time permissions so you have to check for them like I did.

So I am going to have a Button and a EditText. On Buttons click we will open our gallery or any Image Viewer and in the EditText we will set the name of the image we selected.

This line will create a dialog that will ask user for a permission, so we have to handle the user response to the permission as well and also the onActivityResult() method as well after all it will run when you select an image.

AndĀ  you have the image you wanted. You can load this image by converting it in a Bitmap or anything you want to do with it. For uploading this file to server you can follow this link.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • File upload from Android (via Multipart Form data) - Mobikul
    […] Through it you can have a put request to send you file with extra data like in my case I have to send an additional reference tag along with it. So lets start. Here I am supposing that you have a File Adapter and you have selected the file you have to send. Basically you have the URI of the file. If you don’t please follow theĀ  link. […]
  • css.php

    Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.

    Hussein Zawia

    Hussein Zawia

    Co-founder, 24Dokan

    Talk to Sales


    Live Chat
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home