0

I have a parcelable class that is trying to send some camera preview information. So I need to send several byte[] data from one activity to another. I'm using Parcelable with this code:

public class CameraResult implements Parcelable
{   
    private byte[] imagesList; 

    public CameraResult() {}

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(imagesList);
    }

    public byte[] getImageList() {
        return imagesList;
    }

    public void setImageList(byte[] results) {
        this.imagesList = results;
    }


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

    @Override
    public CameraResult[] newArray(int size) {
        return new CameraResult[size];
    }

    @Override
    public CameraResult createFromParcel(Parcel source) {
        CameraResult toReturn = new CameraResult();

        int imageListSize = source.readInt();
        if (imageListSize != 0)
        {
            byte[] tempImageList = new byte[imageListSize];
            source.readByteArray(tempImageList);
            toReturn.setImageList(tempImageList);
        }

      return toReturn;
    }
};
}

This Parcelable class is working fine. But when I try to send a higher amount of data, the communication Intent is not working, as I exceed the maximum data size.

I read this link and this other link with some supposed ways to solve my problem. But I would like to know if it's possible to send higher amounts of data using Parcelable, and if not, which of the mentioned ways is the most adequate one.

Thank you.

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • 1
    As a suggestion,why dont you use 2 fragments with a common activity?That way you cant use the activity for this form communication and there wont be any sending involved.\ – Droidekas Feb 23 '15 at 08:52
  • Fragments is not a possibility due to some reasons why I'm discarding them. – Sonhja Feb 23 '15 at 09:23

1 Answers1

2

Use a singleton or static class to store the shared data.


EDIT:

According to my experience, that's the less painful way to do such a thing, but, also, on the first link you posted it says:

There are several suggestions to achieve this, through defining global variables, static fields, member variables, etc. Having tweaked those ones a bit to get clean encapsulated code, I decided to implement it using Java enum.

It uses a enum as DataHolder:

private enum DataHolder {
    INSTANCE;

    private List<Object> mObjectList;

    public static boolean hasData() {
        return INSTANCE.mObjectList != null;
    }

    public static void setData(final List<Object> objectList) {
        INSTANCE.mObjectList = objectList;
    }

    public static List<Object> getData() {
        final List<Object> retList = INSTANCE.mObjectList;
        INSTANCE.mObjectList = null;
        return retList;
    }
}

I personally prefer to implement a Singleton class as code seems to be more tidy in a different class (I'm a bit obsessed with that), but that's basically the same. Any other solution could be fine as well.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • So you recommend one of the solutions I linked, no way to implement a `Parcelable` that shares big data amount? – Sonhja Feb 23 '15 at 09:24
  • This is a correct answer, it is not a good solution to store data inside the activities, fragments or views – mes Feb 23 '15 at 09:29