Read complete story

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.

. . .

Comment (1)

Add Your Comment

  • 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
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close