Saturday, 24 February 2018

Pick an Image for Android

In this simple tutorial you will learn how to  create an image picker for android !
Creating an image picker for your app is simpler that you might have expected, by using a real working app you can see how you can implement it for your own projects.

Todo list

  •  Add permission WRITE_EXTERNAL_STORAGE
  •  create a button and an imageView
  •  launch an intent on button click
  •  get the result and update the imageView


Permissions : Add this permission to your AndroidManifest.xml :

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

In our Main Activity we have a button and an image view , when we click our button we will prompt our user to select an image :

Intent intent = new Intent(Intent.ACTION_PICK,

startActivityForResult(intent, IMG_RESULT);

because we care about the image our user selected we will retrieve it using onActivityResult :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 try {

    if (requestCode == IMG_RESULT && resultCode == RESULT_OK && null != data) {
// get the selected image

Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
// display the image


} catch (Exception e) {
    Toast.makeText(this,"oops something went wrong", Toast.LENGTH_LONG)

Now our users can select images from their devices using our cool app... Well done!

Get the code on Github

You can see the applications code for reference here :

Happy coding!

No comments:

Post a Comment