Android: Get an image from device

Updated 14 December 2016

Save

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.

author
. . .

Leave a Comment

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


1 comments

  • File upload from Android (via Multipart Form data) - Mobikul
  • Start a Project


      Message Sent!

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

      Back to Home