将Bitmap作为活动之间的成员传递

时间:2014-05-23 00:35:53

标签: android bitmap

ALL,

我有以下课程:

public class Device implements Parcelable
{
private Bitmap photo;
private String bitmapURL;
private char sex;
private String fname;
private String lname;
private String phone;
private double latitude;
private double longtitude;
private ArrayList<Message> messages = new ArrayList<Message>();
private boolean isFriend;
private boolean doNotDisturb = false;

Device()
{   
    setDoNotDisturb(false);
}

Device(String a, char b, String c, String d, String e, double f, double g, boolean h)
{
    bitmapURL = a;
    sex = b;
    fname = c;
    lname = d;
    phone = e;
    latitude = f;
    longtitude = g;
    isFriend = h;
    setDoNotDisturb(false);
}

@SuppressWarnings("unchecked")
private Device(Parcel in)
{
    photo = (Bitmap) in.readParcelable( Bitmap.class.getClassLoader() );
    sex = (char) in.readInt();
    fname = in.readString();
    lname = in.readString();
    phone = in.readString();
    latitude = in.readDouble();
    longtitude = in.readDouble();
    messages = in.readArrayList( Device.class.getClassLoader() );
    bitmapURL = in.readString();
    isFriend = in.readInt() == 1 ? true : false;
    doNotDisturb = in.readInt() == 1 ? true : false;
}

@Override
public void writeToParcel(Parcel dest, int flags)
{
    dest.writeValue( photo );
    dest.writeInt( sex );
    dest.writeString( fname );
    dest.writeString( lname );
    dest.writeString( phone );
    dest.writeDouble( latitude );
    dest.writeDouble( longtitude );
    dest.writeList( messages );
    dest.writeString( bitmapURL );
    dest.writeInt( isFriend ? 1 : 0 );
    dest.writeInt( doNotDisturb ? 1 : 0 );
}

}

然后我有以下代码:

在Activity1.java中:

Intent intent = new Intent( getApplicationContext(), PhotoTaker.class );
intent.putExtra( "device", device );
startActivity( intent );

在PhotoTaker.java中,我有以下代码:

device.setPhoto( bmp  );
Intent intent = new Intent( context, SexSelector.class );
intent.putExtra( "device", device );
startActivity( intent );

现在,Activity1中的代码可以正常工作,而PhotoTaker中的代码却没有。没有例外,CatLog中没有任何内容。它只是默默地崩溃,代码返回到Activity1,而不是启动SexSelector活动。

有人知道发生了什么吗?我如何修复代码,以便代码将遵循SexSelector活动?

我可以在Device构造函数中创建一个Bitmap对象,但是我不知道如何在实际对象上设置合适的Bitmap。然后,我可以使用photo.writeToParcel()将位图写入parcel,但在这种情况下,我只会让内存泄漏。

谢谢。

0 个答案:

没有答案