0
我將類接口更改爲使用parcelable,因爲我需要通過一些對象來通過一些活動才能完成我的工作。獲取具有可調節槽活動的特定字段
所以,我實現了一個parcelable類(使用一個插件,這是否)這樣的:
public class Photo implements Parcelable {
private int id;
private Uri image;
private byte[] cropedImage;
private String path;
private Double lat;
private Double lon;
private Double alt;
private String time;
public Photo() {
}
@Override
public int describeContents() {
return 0;
}
public Photo(Uri image, Double lat, Double lon, Double alt, String time) {
this.image = image;
this.lat = lat;
this.lon = lon;
this.alt = alt;
this.time = time;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeParcelable(this.image, flags);
dest.writeByteArray(this.cropedImage);
dest.writeString(this.path);
dest.writeValue(this.lat);
dest.writeValue(this.lon);
dest.writeValue(this.alt);
dest.writeString(this.time);
}
protected Photo(Parcel in) {
this.id = in.readInt();
this.image = in.readParcelable(Uri.class.getClassLoader());
this.cropedImage = in.createByteArray();
this.path = in.readString();
this.lat = (Double) in.readValue(Double.class.getClassLoader());
this.lon = (Double) in.readValue(Double.class.getClassLoader());
this.alt = (Double) in.readValue(Double.class.getClassLoader());
this.time = in.readString();
}
public static final Creator<Photo> CREATOR = new Creator<Photo>() {
@Override
public Photo createFromParcel(Parcel source) {
return new Photo(source);
}
@Override
public Photo[] newArray(int size) {
return new Photo[size];
}
};
}
我激活Google創建和填寫我的照片像這樣的構造器:
Photo photo = new Photo(image,location.getLatitude(),location.getLongitude(),location.getAltitude(),data);
Intent i = new Intent(CameraCapture.this, CropImage.class);
i.putExtra("Photo",photo);
我把它傳遞給CropImage活動,並在該活動中,我需要檢索parcelable並獲取具體數據(在本例中只是uri)
這是我做了什麼:
photo = getIntent().getExtras().getParcelable("Photo");
uri = photo.getImage();
中的getImage()不存在,我不知道如何以檢索的parcelable領域特定的照片對象,任何幫助嗎?有沒有其他方式可以使用parcelable來做到這一點,我不知道?
非常感謝
哦,這與它有區別,謝謝很多 – afcosta007