您的位置:首页 > 其它

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

2017-12-29 01:08 330 查看
首先:我们需求确定的思路是,用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,否则会闪退,报错:

完毕,够详细了吧~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: