android activity之间传递bitmap

本文详细介绍了如何使用Intent在Android应用间传递数据,包括基本数据类型、可序列化对象和bitmap的传递方法。通过实例展示了序列化Student类、将bitmap转换为字节数组进行传递的过程,并在目标活动中正确接收数据。

activity之间有个很好的传递数据的工具–>Intent。

传递基本数据类型

如果是八大基本数据类型,可以直接传递:

Intent intent = new Intent(activityA,activityB);
intent.putExtra("key1",111);
startActivity(intent);

在activityB中接收

Intent intent = getIntent();
int value1 = intent.getInt("key1",1);

传递可序列化对象

序列化Student类

public class Student implements Serializable{

    private String username;
    private int age;
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return this.username;
    }
    public void setAge(){
        this.age = age;
    }
    public int getAge(){
        return this.age;
    }
}

activityA中发送数据

Student student = new Student();
student.setUsername("大飞");
student.setAge(18);
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("student",student);
intent.putExtras(bundle);
startActivity(activityA,activityB); 

在activityB中接收数据

Student student = (Student)getIntent.getExtras("student");

传递bitmap

思路:将bitmap对象转换为字节数组传递
activityA发送数据:

Intent intent = new Intent(activityA,activityB);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//下面方法表示压缩图片,中间的值越小,压缩比例越大,失真也约厉害,100表示不压缩
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] byteArray = baos.toByteArray();
intent.putExtra("photoByte",byteArray);
startActivity(intent);

activityB接收数据

byte[] byteArray = getIntent().getByteArrayExtra("photoByte");
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值