Working with Android ImageView and Bitmap

android.widget.ImageView is the main class to handle image files. ImageView loads, optimizes and displays image. Android devices come in different screens and dpi, which make it easy to implement but hard to master image in Android. To make it simple, I won’t go discuss about screen resolution of different Android devices in this tutorial.

The simple way

Upload an PNG file to your project’s drawable folder then add an ImageView as following.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.androidnames.imageview.MainActivity">

<ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="50dp" android:src="@drawable/Androidnames_logo" />



That was the most simple way to load an image into an ImageView. The view can display image differently by using android:scaleType.

<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" android:src="@drawable/Androidnames_logo" />

The code above displays the Androidnames_logo.png image at its native resolution and centered in the view. The full list of scaleType’s values can be found here.

In some cases, it is required to load or switch the image after an event. ImageView’s setImageResource method helps make this task done.


ImageView imageView = (ImageView) findViewById(;


Loading image with Bitmap

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources, be it file path, input stream, or bitmaps.

XML inflation

ImageView imageView = (ImageView) findViewById(;
Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.Androidnames_logo);

From File

//use this when you need to load an image from sdcard
ImageView imageView = (ImageView) findViewById(;
Bitmap bitMap = BitmapFactory.decodeFile("/sdcard/another_image.png");

From Input stream

[sourcecode language=”java”]
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/sdcard/another_image.png");
buf = new BufferedInputStream(in);
//there are 2 ways to handle the input stream
// first one is to use decodeStream
Bitmap bitMap = BitmapFactory.decodeStream(buf);
//the 2nd one is to create bitmap from an array of bytes. Use this when bitmap was already created previously in your project
byte[] bitMapArray= new byte[buf.available()];;
Bitmap bitMap2 = BitmapFactory.decodeByteArray(bitMapArray, 0, bitMapArray.length);

imageView.setImageBitmap(bitMap); //or bitMap2

if (in != null) {
if (buf != null) {
} catch (Exception e) {
Log.e("Error reading file", e.toString());

Download Image from URL

In many apps, it is required to download image from internet and load it into an ImageView. There are many ways to carry out this task.

No matter what method you use, the first step is always to allow INTERNET permission in manifest file.

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

Use image loader LIBRARIES

If you want to download an image from internet and load it to an ImageView, you should use an image loader library like Picasso library.

Some examples:

ImageView imageView = (ImageView) findViewById(;

Use Bitmap

try {
URL url = null;
url = new URL("");

Bitmap bitmap;
bitmap= BitmapFactory.decodeStream(url.openConnection().getInputStream());
ImageView imageView = (ImageView) findViewById(;
} catch (MalformedURLException e) {

Leave a Comment

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

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.