BItMap in Andoid

Introduction

 
This article explains BitMaps in Android. Android Studio is used to create the sample.
 
When we download images they come in many sizes that we want for the user interface. When you load an image from the internet, sometimes it might happen that an outOfMemoryException occurs due to the large size of an image. Android phone memory is very limited so we should not use any image that is larger than needed. This application will show you how to load a large image into memory without the outOfMemoryException.
 
Step 1
 
Create a project like this:
 
resize2
 
Click "Next".
 
resize3
 
Click "Next".
 
resize4
 
Click "Next".
 
resize5
 
Click "Finish".
 
Step 2
 
Create an XML file with the following.
 
In the XML file, you will use the imageView inside the RelativeLayout. ImageView that contains the image after resizing.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <ImageView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/imageview"  
  15.        />  
  16.    
  17.     <Button  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:id="@+id/button1"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:layout_marginTop="300dp"  
  23.         android:text="Button"  
  24.         />  
  25. </RelativeLayout> 
Step 3
 
Create a Java class file with the following.
 
In the Java class file, you will use the image into the bitmap using the BitmapFactory class. Create a method named setSize and write the code to change the size of an image. This method will use an imagereal parameter. The size of the image that you want after resizing and filtering. This method will return a bitmap image that you will use in the BitMap variable. Now set the BitMap image in an imageView.
  1. package com.bitmapandroid;  
  2.    
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11. import android.widget.Toast;  
  12.    
  13. public class MainActivity extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.    
  16.     static int width;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         Button button1=(Button)findViewById(R.id.button1);  
  22.         ImageView image = (ImageView) findViewById(R.id.imageview);  
  23.         final Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.hotel);  
  24.         Bitmap bitmap=setSize(bMap,200,true);  
  25.         image.setImageBitmap(bitmap);  
  26.         button1.setOnClickListener(new View.OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 Toast.makeText(getApplicationContext(),""+width,Toast.LENGTH_LONG).show();  
  30.             }  
  31.         });  
  32.     }  
  33.     public static Bitmap setSize(Bitmap Imagereal, float ImageSizemax,  
  34.                                  boolean filter) {  
  35.         float ratio = Math.min(  
  36.                 (float) ImageSizemax / Imagereal.getWidth(),  
  37.                 (float) ImageSizemax / Imagereal.getHeight());  
  38.         width = Math.round((float) ratio * Imagereal.getWidth());  
  39.         int height = Math.round((float) ratio * Imagereal.getHeight());  
  40.         Bitmap newBitmap = Bitmap.createScaledBitmap(Imagereal, width,  
  41.                 height, filter);  
  42.         return newBitmap;  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.main, menu);  
  48.         return true;  
  49.     }  
Step 4
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.bitmapandroid"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.bitmapandroid.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 5
 
resize