http://blog.csdn.net/shenyu123/article/details/7264963
基本思路是先把bitmap转化为byte数组,用Intent传递数组,在将数组转化为bitmap
bitmap转化为byte数组的方法:
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte数组转化为bitmap方法:
byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
程序实例:
第一个activity:
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SendImageActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Bitmap bitmap;
byte buff[] = new byte[125*250];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView mImageView = (ImageView) findViewById(R.id.image);
Button bt1 = (Button) findViewById(R.id.bt1);
bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.option24);
buff = Bitmap2Bytes(bitmap);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
mImageView.setBackgroundDrawable(mBitmapDrawable);
bt1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent mIntent = new Intent();
mIntent.putExtra("image", buff);
mIntent.setClass(this, activity2.class);
startActivity(mIntent);
}
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
第二个activity:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class activity2 extends Activity {
private Bitmap bitmap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ImageView mImageView = (ImageView) findViewById(R.id.image2);
Intent mIntent = getIntent();
byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
mImageView.setBackgroundDrawable(mBitmapDrawable);
}
}