It's a example to save and read bitmap in Internal Storage. In AndroidBitmapActivity class, the bitmaps are saved in Internal Storage, in MODE_PRIVATE (where the created file can only be accessed by the calling application). The bitmaps will be read in SecondActivity class.
AndroidBitmapActivity.java
package com.exercise.AndroidBitmap;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidBitmapActivity extends Activity {
ImageView image1, image2, image3, image4;
Button buttonSwitch;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
image4 = (ImageView)findViewById(R.id.image4);
buttonSwitch = (Button)findViewById(R.id.switchactivity);
Bitmap bmOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
image1.setImageBitmap(bmOriginal);
int width = bmOriginal.getWidth();
int height = bmOriginal.getHeight();
final Bitmap bmDulicated2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Bitmap bmDulicated3 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Bitmap bmDulicated4 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] srcPixels = new int[width * height];
bmOriginal.getPixels(srcPixels, 0, width, 0, 0, width, height);
int[] destPixels = new int[width * height];
swapGB(srcPixels, destPixels);
bmDulicated2.setPixels(destPixels, 0, width, 0, 0, width, height);
image2.setImageBitmap(bmDulicated2);
swapRB(srcPixels, destPixels);
bmDulicated3.setPixels(destPixels, 0, width, 0, 0, width, height);
image3.setImageBitmap(bmDulicated3);
swapRG(srcPixels, destPixels);
bmDulicated4.setPixels(destPixels, 0, width, 0, 0, width, height);
image4.setImageBitmap(bmDulicated4);
buttonSwitch.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
FileOutputStream fos;
try {
fos = openFileOutput("BITMAP_A", Context.MODE_PRIVATE);
bmDulicated2.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
fos = openFileOutput("BITMAP_B", Context.MODE_PRIVATE);
bmDulicated3.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
fos = openFileOutput("BITMAP_C", Context.MODE_PRIVATE);
bmDulicated4.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(AndroidBitmapActivity.this, SecondActivity.class);
startActivity(intent);
}});
}
void swapGB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xffff0000)
| ((src[i] & 0x000000ff)<<8)
| ((src[i] & 0x0000ff00)>>8);
}
}
void swapRB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff00ff00)
| ((src[i] & 0x000000ff)<<16)
| ((src[i] & 0x00ff0000)>>16);
}
}
void swapRG(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff0000ff)
| ((src[i] & 0x0000ff00)<<8)
| ((src[i] & 0x00ff0000)>>8);
}
}
}
SecondActivity.java
package com.exercise.AndroidBitmap;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class SecondActivity extends Activity {
ImageView imageA, imageB, imageC;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
imageA = (ImageView)findViewById(R.id.imagea);
imageB = (ImageView)findViewById(R.id.imageb);
imageC = (ImageView)findViewById(R.id.imagec);
Bitmap bitmapA = null;
Bitmap bitmapB = null;
Bitmap bitmapC = null;
FileInputStream fis;
try {
fis = openFileInput("BITMAP_A");
bitmapA = BitmapFactory.decodeStream(fis);
fis.close();
fis = openFileInput("BITMAP_B");
bitmapB = BitmapFactory.decodeStream(fis);
fis.close();
fis = openFileInput("BITMAP_C");
bitmapC = BitmapFactory.decodeStream(fis);
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageA.setImageBitmap(bitmapA);
imageB.setImageBitmap(bitmapB);
imageC.setImageBitmap(bitmapC);
}
}
Download the files.
Related article:
- Share Bitmap between activities as global common resources
- Internal Storage
No comments:
Post a Comment