3

I want to pass an object of type Annonce to an Intent. As you can see, it's simple if the class attributes are primitives, However; in my case I have an image (Bitmap) and an attribute of type Client ( I have created a Client class). My solution is to access the Client attributes (using getter and setter) and parsing it in the writeToParcel method one by one (it takes too much time), and for the image, I am sending it in the mainActivity using ByteArrayOutputStream. Can anyone help me do it all in Annonce class.

public class Annonce implements Parcelable {

    String article, desc, temps, ville, categorie;
    int prix;
    Bitmap img;
    Client c;

    public Annonce(String article, String desc, String temps, String ville,
            String categorie, int prix, Bitmap img, Client c) {
        this.article = article;
        this.desc = desc;
        this.c = c;
        this.prix = prix;
        this.img = img;
        this.temps = temps;
        this.categorie = categorie;
        this.ville = ville;
    }

    public static final Parcelable.Creator<Annonce> CREATOR = new Parcelable.Creator<Annonce>() {

        public Annonce createFromParcel(Parcel source) {
            return new Annonce(source);
        }

        public Annonce[] newArray(int size) {
            return new Annonce[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(article);
        parcel.writeString(desc);
        parcel.writeString(temps);
        parcel.writeString(ville);
        parcel.writeString(categorie);
        parcel.writeInt(prix);
    }

    public Annonce(Parcel source) {
        article = source.readString();
        desc = source.readString();
        temps = source.readString();
        ville = source.readString();
        categorie = source.readString();
        prix = source.readInt();
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
ghassen92
  • 305
  • 1
  • 2
  • 12
  • How about a Serializable approach? – Sheychan Jul 16 '15 at 01:24
  • is there any solution with parcelable ? – ghassen92 Jul 16 '15 at 01:25
  • In parcelable you can just convert the Client into Object and vice versa. however Bitmap can never be passed.. it should be a byte[] first before going to bitmap – Sheychan Jul 16 '15 at 01:28
  • Just in case, how huge are those bitmaps usually? – user1643723 Jul 16 '15 at 01:30
  • u mean c= (Client) source.readValue(ClassLoader.getSystemClassLoader()); – ghassen92 Jul 16 '15 at 01:30
  • the bitmap are big , it only work with new smartphone , but using my old phone, it stopped if i send a hd image . – ghassen92 Jul 16 '15 at 01:32
  • parcel.writeValue(Object); – Sheychan Jul 16 '15 at 01:32
  • And in images you have parcel.writeByteArray(); doesnt it? :) – Sheychan Jul 16 '15 at 01:34
  • Tell me if it works Ill build an answer – Sheychan Jul 16 '15 at 01:35
  • This is not even question of "big". AFAIK the *total* size of Binder buffer (which holds all Parcelables in all Binder interactions of your process) is about 1Mb. This means, that as soon, as someone equally "clever" sends you another image or byte array of similarly huge size (but still below 1Mb) your app will choke with TransactionTooLargeException. Don't send Bitmaps over the wire. This is stupid design mistake. Send Uri. Retrive the bitmap from Uri on other side. – user1643723 Jul 16 '15 at 01:40
  • In serializable approach you may write on the disk for the bitmap.. then read it anywhere – Sheychan Jul 16 '15 at 01:42
  • @Sheychan , it dosn't work, img is bitmap not []bytes – ghassen92 Jul 16 '15 at 01:42
  • Yah that is why you should convert it into bytes[] its very possible... http://stackoverflow.com/questions/4989182/converting-java-bitmap-to-byte-array – Sheychan Jul 16 '15 at 01:43
  • Then when you already need the bitmap convert the byte[] into bitmap http://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to-bitmap – Sheychan Jul 16 '15 at 01:43
  • I convert it in Annonce class ? – ghassen92 Jul 16 '15 at 01:45
  • 1
    Don't do it in `Annotate`, do your disk operations in `AsyncTask`. Also please move the extended discussion into the chat. This is not what comments on Stack Overflow are for. – user1643723 Jul 16 '15 at 01:48

1 Answers1

0

Having an attribut of type "bitmap" is not a good solution . Instead of that , we can use the path of the image to refer to the bitmap image . Also, we can convert the Client into object in parcelable in order to send it through intent.

ghassen92
  • 305
  • 1
  • 2
  • 12