Passing Bitmap To Another Activity

This example explains how you can pass a bitmap from one activity to another.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PassBitmapToActivity.

2.) Create and write following into AndroidReceivedBitmap.java:

 

package com.example.passbitmaptoactivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidReceiveBitmap extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

setContentView(R.layout.newimage);
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);

viewBitmap.setImageBitmap(bitmap);

}

}

 

 

3.) Create and write following into newimage.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/bitmapview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

 

 

4.) Add following activity into your manifest file:

 

<activity android:name=".AndroidReceiveBitmap"
android:label="AndroidReceiveBitmap">
</activity>

 

 

5.) Run for output.

Steps:

1.) Create a project named PassBitmapToActivity and set the information as stated in the image.

Build Target: Android 4.4
Application Name: PassBitmapToActivity
Package Name: com.example.PassBitmapToActivity
Activity Name: PassBitmapToActivityActivity

passbitmap1

2.) Open PassBitmapToActivityActivity.java file and write following code there:

 

package com.example.passbitmaptoactivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class PassBitmapToActivityActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

Intent intent = new Intent();
intent.setClass(PassBitmapToActivityActivity.this, AndroidReceiveBitmap.class);
intent.putExtra("Bitmap", bitmap);
startActivity(intent);

}
}

 

 

3.) Compile and build the project.

Output

passbitmap2

passbitmap3