Reputation: 3112
I had some problems passing a ArrayList<People>
(which contains large pictures) over Activities using Bundle
. I failed because Bundle
has a 1MB limit.
With some research, i found out that it would be better Serializing the object and saving it to a file, so I made this class:
public class BundlePeople {
public static long packPeople(Context context, List<People> people) {
long timeToken = System.currentTimeMillis();
String fileName = "temp_" + Long.toString(timeToken);
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(people);
os.close();
fos.close();
} catch (IOException e) {
Log.e("BundlePeople", e.getMessage());
}
return timeToken;
}
public static List<People> unpackPeople(Context context, long id) {
List<People> list = new ArrayList<>();
String fileName = "temp_" + Long.toString(id);
try {
FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
list.addAll((List<People>) is.readObject()); //Here I get NotSerializableException on second call
is.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
Log.e("BundlePeople", e.getMessage());
}
return list;
}
}
It works once, but when I try use it again, it throws NotSerializableExcpetion
First call:
Passing (FirstActivity.java)
Bundle bundle = new Bundle();
bundle.putLong("peopleFileId", BundlePeople.packPeople(FirstActivity.this, listPeople));
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtras(bundle);
startActivityForResult(i.setFlags(SecondActivity.FLAG_SELECT), 0);
Receiving (SecondActivity.java)
long id = getIntent().getExtras().getLong("peopleFileId");
list = new ArrayList<>();
list.addAll(BundlePeople.unpackPeople(getApplicationContext(), id));
It works fine for now, the problem is when I use it for the second time:
Second call:
Passing (SecondActivity.java)
Intent i = new Intent();
Bundle b = new Bundle();
b.putLong("peopleFileId", BundlePeople.packPeople(SecondActivity.this, list));
i.putExtras(b);
setResult(REQUEST_PEOPLE, i);
finish();
Receiving (FirstActivity.java [onActivityResult()])
List<People> list = BundlePeople.unpackPeople(
getApplicationContext(), data.getExtras().getLong("peopleFileId"));
Here my list is null
because an exception is thrown on BundlePeople
at the line indicated on first code.
Exception message: Read an exception; java.io.NotSerializableException: br.com.something.activities.SecondActivity$1
My questions is, why am I getting this exception? Is this the better way to pass large objects over activities? If it is, what's wrong here?
EDIT
People.java
public class People implements Serializable {
private static final long serialVersionUID = -9171517741542003990L;
private Integer id;
private String name;
private BitmapSerializable image;
public BitmapSerializable getImage() {
return image;
}
public void setImage(BitmapSerializable image) {
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Upvotes: 0
Views: 719
Reputation: 310913
You:
Fortunately Java writes the exception to the file in this circumstance and threw it when you deserialized. The exception message gives you the name of the class concerned.
But it should never have got this far.
Upvotes: 3