Search code examples
javaandroidserializationparcelable

Get specific field with parcelable trough activities


I changed my class interface to use parcelable, since i need to pass a object trough some activities to have my work done.

So i have implemented a parcelable class(using a plugin that does that) like that:

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

on my activite i create and fill the contructor of my photo like this:

  Photo photo = new Photo(image,location.getLatitude(),location.getLongitude(),location.getAltitude(),data);

        Intent i = new Intent(CameraCapture.this, CropImage.class);
        i.putExtra("Photo",photo);

i pass it to the CropImage activitie, and on that activity i need to retrive the parcelable and get the specific data(in this case just the uri)

this is what i done:

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

the getImage() doesn't exist, i don't know how to retrive the parcelable field for the specific photo object, any help? is there other way to do this using parcelable that i am not figuring out?

Thanks a lot


Solution

  • As i see your data class does not have getters and setters , so try right clicking inside that class and selecting create setters and getters . and then use those getters to fetch your data

    for ex. if u wish to get time

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