Android decode image

If you use a lot of images in your application, knowing how to load the big images effectively is important to reduce memory consumption and increase the application's performance. Typically an image produced by Camera application is higher resolution than the screen density of your device. Loading a big image into a smaller UI component is not beneficial at all, but still takes up memory and degrades performance. For example, it is not good to load a 1200x1600 pixel image into memory if it will eventually be displayed in a 300x450 ImageView. You will use a lower resolution version of the image.
To achieve this goal, the image must be decoded. The BitmapFactory class provides different methods to decode the image from various sources. Its decodeByteArray() method allows you to decode an image from a byte array. The decodeFile() method decodes an image from a file. And the decodeResource() method decodes an image from a resource image id. When decoding an image, you specify a sample size. The sample size is the number of pixels in the source image that correspond to a single pixel in the decoded image. For example, sample size == 2 returns an image containing ¼ (1/2 width*1/2 height) the number of pixels. A sample size that is less than or equal to 1 is treated the same as 1. A good sample size is calculated with taking into account the dimensions of the source image and those of the UI component (e. g. ImageView) to display image. For example, a sample size should be a smallest ratio of width ratio and height ratio. The width ratio is the result of the division operation of the width of the source image by the requested width. The height ratio is the result of the division operation of the height of the source image by the requested height.

Now to have an example application on decoding image, create a new Android project. Then modify the activity_main.xml file to add an ImageView as shown below.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.andexample.MainActivity" >

   <ImageView
     android:id="@+id/imgview"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     />

</RelativeLayout>


Here is the MainActivity class. The code is written to decode an image stored in the res/drawable folder and display the decoded image in the ImageView.
package com.example.andexample;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
//import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics=new DisplayMetrics();
//get screen size (width and height)
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth=metrics.widthPixels;
int screenHeight=metrics.heightPixels;
ImageView imgView=(ImageView)findViewById(R.id.imgview);
Bitmap decodedImage=decode(screenWidth-15,screenHeight-15);
imgView.setImageBitmap(decodedImage);
}

public Bitmap decode( int width,int height){
BitmapFactory.Options options = new BitmapFactory.Options();
// Get the width and height of the image without allocating the memory
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.img2014, options);
int imgHeight = options.outHeight;
int imgWidth = options.outWidth;
//default sample size
int sampleSize=1;
// Calculate a proper sample size
if(width< imgWidth || height<imgHeight ){
int widthRatio=Math.round(imgWidth/(float)width);
int heightRatio=Math.round(imgHeight/(float)height);
// Select the smallest ratio to make sure that the final image with both dimensions larger than or equal to the
     // requested height and width.
sampleSize=widthRatio<heightRatio?widthRatio:heightRatio;
}
// Decode the image with the specified sample size
options.inJustDecodeBounds = false;
options.inSampleSize=sampleSize;
return BitmapFactory.decodeResource(getResources(), R.drawable.img2014, options);


}

}

Android-decode-image

Posted by: Dara | post date: 10-10-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.