通过可分割的槽活动获取特定字段

Get specific field with parcelable trough activities

我更改了我的 class 界面以使用 parcelable,因为我需要通过一些活动传递一个对象来完成我的工作。

所以我实现了一个 parcelable class(使用一个插件),就像这样:

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];
        }
    };
}

在我的活动中,我创建并填充了我的照​​片的构造函数,如下所示:

  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 活动,然后 activity 我需要检索 parcelable 并获取特定数据(在本例中只是 uri)

这是我做的:

photo = getIntent().getExtras().getParcelable("Photo");
uri = photo.getImage();

getImage() 不存在,我不知道如何检索特定照片对象的 parcelable 字段,有帮助吗?有没有其他方法可以使用我不知道的 parcelable 来做到这一点?

非常感谢

我看到你的数据 class 没有 getters 和 setters ,所以尝试在 class 里面右击并选择创建 setters 和 getters 。然后使用这些 getter 获取您的数据

例如。如果你想得到时间

private String time;
 public String getTime(){    
    return time;
 }