4

In my application I'm sending data to my Activity through Intent extras.

In my specific case, I download a JSON file, convert its content to a String, and send it as Intent extra to my Activity. The size is about 500kB.

And I get a TransactionTooLargeException.

09-28 13:40:46.647: E/JavaBinder(441): !!! FAILED BINDER TRANSACTION !!!
09-28 13:40:46.647: W/ActivityManager(441): Exception in new application when starting activity com.vektor.sourfer/.SourceActivity
09-28 13:40:46.647: W/ActivityManager(441): android.os.TransactionTooLargeException
09-28 13:40:46.647: W/ActivityManager(441):     at android.os.BinderProxy.transact(Native Method)
09-28 13:40:46.647: W/ActivityManager(441):     at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:723)
09-28 13:40:46.647: W/ActivityManager(441):     at com.android.server.am.ActivityStack.realStartActivityLocked(ActivityStack.java:716)
09-28 13:40:46.647: W/ActivityManager(441):     at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:4341)
09-28 13:40:46.647: W/ActivityManager(441):     at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:4405)
09-28 13:40:46.647: W/ActivityManager(441):     at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:390)
09-28 13:40:46.647: W/ActivityManager(441):     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1737)
09-28 13:40:46.647: W/ActivityManager(441):     at android.os.Binder.execTransact(Binder.java:388)
09-28 13:40:46.647: W/ActivityManager(441):     at dalvik.system.NativeStart.run(Native Method)
09-28 13:40:46.647: W/ActivityManager(441): Force removing ActivityRecord{421bbed8 u0 com.vektor.sourfer/.SourceActivity}: app died, no saved state

In cases like this, which is the best way to bypass this error?

Vektor88
  • 4,841
  • 11
  • 59
  • 111

3 Answers3

4

One solution would be to save the data into a public static member variable and then access it from the second activity.

Another solution would be to save the result into a file on external storage, and then read it from that file.

ReggieB
  • 8,100
  • 3
  • 38
  • 46
Andy Res
  • 15,963
  • 5
  • 60
  • 96
4

There is a limit of 1MB on transferred data size but it's actually smaller and device specific. How are you transferring this String? via putExtra(String string) method? You should try to wrap it as a Serializable since it will do some smart things to use just one byte per character if possible. But if your string can be larger than this than you're too close to the limit so you should probable save it to disk and not transfer at all.

I've written a blog post about this subject that might be interesting for anyone getting this exception.

Nemanja Kovacevic
  • 3,510
  • 2
  • 30
  • 45
1

First Approach: Since you are using Json, use JsonReader

https://sites.google.com/site/gson/streaming

This code reads a JSON document containing an array of messages. It steps through array elements as a stream to avoid loading the complete document into memory. It is concise because it uses Gson’s object-model to parse the individual messages:

public List<Message> readJsonStream(InputStream in) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    List<Message> messages = new ArrayList<Message>();
    reader.beginArray();
    while (reader.hasNext()) {
        Message message = gson.fromJson(reader, Message.class);
        messages.add(message);
    }
    reader.endArray();
    reader.close();
    return messages;
}

Second approach: Save the file and pass the file URI/path to second activity.

Tarun
  • 13,727
  • 8
  • 42
  • 57