Android Image Viewer Displaying the Image

The android image viewer displaying the image tutorial is the forth part to the android image viewer tutorial series.

Where the viewers will be shown how to create a basic android application that  loads an image thumbnail and then displays it on the full screen.

This android tutorial series targeted towards beginners to android who would like to learn how to create a basic android application.

In this tutorial we show how to display the image with the uri that was returned in the previous tutorial.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/android_image_viewer.git Tag image-viewer-setimagebitmap

or you can run this command

git clone https://github.com/mobapptuts/android_image_viewer.git –branch image-viewer-setimagebitmap

Code Samples

Creating a getBitmapFromUri method

This method describes how to get the bitmap from the Uri which was explained in the previous tutorial.

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
       ParcelFileDescriptor parcelFileDescriptor =
               getContentResolver().openFileDescriptor(uri, "r");
       FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
       Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
       parcelFileDescriptor.close();
       return image;
   }

Calling the getBitmapFromUri from inside the onActivityResult method

@Override
   public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
       if(requestCode == REQUEST_FOLDER_CODE && resultCode == RESULT_OK) {
           Uri uri = null;
           if(resultData != null) {
               uri = resultData.getData();
               try {
                   Bitmap bitmap = getBitmapFromUri(uri);
                   imageView.setImageBitmap(bitmap);
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
   }

Android Image Viewer Displaying the Image Summary

In this tutorial we learned

  • How to get a bitmap from uri using the ParcelFileDescriptor & FileDescriptor
  • How to set the bitmap for an ImageView
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>