用Intent将图片发送到另外一个Activity(用bitmap)

首先:我们需求确定的思路是,用Intent将图片发送到另外一个activity,,第一个页面先将bitmap位图转化成byte字节数组,然后发送到第二个activity页面,第二个页面将byte数组再转化成bitmap位图数组,即可在第二个页面显示出发送过来的图片:话不多说,上代码图:


第一个页面的JAVA代码:(自己把包导入进去)

public class MainActivity extends Activity implements OnClickListener {
    private Bitmap bitmap;  
    byte buff[] = new byte[125*250];  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        ImageView mImageView = (ImageView) findViewById(R.id.image);  
        Button bt1 = (Button) findViewById(R.id.bt1);  
        bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.accept);  
        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, seconde.class);  
        startActivity(mIntent);  
    }  
      
    private byte[] Bitmap2Bytes(Bitmap bm){     
        ByteArrayOutputStream baos = new ByteArrayOutputStream();       
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);       
        return baos.toByteArray();     
       }    
}  


第一个页面的XML代码:里面就一个ImageView,和Button

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="50dp"
        android:text="Button" />
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_centerVertical="true"/>        

<--imageview注意这里不要添加src或者background,否则会发送两个图片过去-->


第二个页面的JAVA代码:

public class seconde  extends Activity{
private Bitmap bitmap;  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        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);  
          
    }  
}


第二个页面的XML代码:

<ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



记住:因为第二个second类是一个Activity,所以需要在androidManifest注册activity,否则会闪退,报错:


完毕,够详细了吧~~


  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值