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 a large size of an image. Android phone memory is very limited so we should not be 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.

<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"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">

 

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageview"

       />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button1"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="300dp"

        android:text="Button"

        />

</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.
 

package com.bitmapandroid;

 

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;

 

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

 

    static int width;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button button1=(Button)findViewById(R.id.button1);

        ImageView image = (ImageView) findViewById(R.id.imageview);

        final Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.hotel);

        Bitmap bitmap=setSize(bMap,200,true);

        image.setImageBitmap(bitmap);

        button1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),""+width,Toast.LENGTH_LONG).show();

            }

        });

    }

    public static Bitmap setSize(Bitmap Imagereal, float ImageSizemax,

                                 boolean filter) {

        float ratio = Math.min(

                (float) ImageSizemax / Imagereal.getWidth(),

                (float) ImageSizemax / Imagereal.getHeight());

        width = Math.round((float) ratio * Imagereal.getWidth());

        int height = Math.round((float) ratio * Imagereal.getHeight());

        Bitmap newBitmap = Bitmap.createScaledBitmap(Imagereal, width,

                height, filter);

        return newBitmap;

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

}


Step 4

Android Manifest.xml file
 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.bitmapandroid"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="7"

        android:targetSdkVersion="16" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.bitmapandroid.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

Step 5

resize

 

Up Next
    Ebook Download
    View all
    Learn
    View all