Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max size of string data that can be passed in intents [duplicate]

Tags:

android

Is there a max limit to the string data that can be passed in intent extra? How much data can the below String str hold?

intentI1.putExtra("MyString", str);
like image 272
user1930106 Avatar asked Feb 25 '15 21:02

user1930106


People also ask

What is the maximum amount of data that you can transfer using intent?

When using intent to transfer data, 1MB is not the upper security limit. Because other transport work may be in progress in binder. The upper limit may be different for different models and system versions.

How much data can a String hold?

Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.

How much data can a String hold in Java?

So you can have a String of 2,147,483,647 characters, theoretically.

How do you pass data between intents?

To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.


3 Answers

My tests on Android API Level 24:

            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//            intent.putExtra("extra", new byte[1024 * 1024]); // 1024 KB = 1048576 B, android.os.TransactionTooLargeException
//            intent.putExtra("extra", new byte[1024 * 512]); // 512 KB = 524288 B, android.os.TransactionTooLargeException
//            intent.putExtra("extra", new byte[1024 * 506]); // 506 KB = 518144 B, android.os.TransactionTooLargeException
//            intent.putExtra("extra", new byte[1024 * 505]); // 505 KB = 517120 B, android.os.TransactionTooLargeException
            intent.putExtra("extra", new byte[1024 * 504]); // 504 KB = 516096 B, OK
            startActivity(intent);

android.os.TransactionTooLargeException https://developer.android.com/reference/android/os/TransactionTooLargeException.html

Because I need to send large amount of data to the activity, I am using the following solution (i know, its not perfect, but it can help):

public class ExtendedDataHolder {

    private static ExtendedDataHolder ourInstance = new ExtendedDataHolder();

    private final Map<String, Object> extras = new HashMap<>();

    private ExtendedDataHolder() {
    }

    public static ExtendedDataHolder getInstance() {
        return ourInstance;
    }

    public void putExtra(String name, Object object) {
        extras.put(name, object);
    }

    public Object getExtra(String name) {
        return extras.get(name);
    }

    public boolean hasExtra(String name) {
        return extras.containsKey(name);
    }

    public void clear() {
        extras.clear();
    }

}

Then in MainActivity

ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
extras.putExtra("extra", new byte[1024 * 1024]);
extras.putExtra("other", "hello world");

startActivity(new Intent(MainActivity.this, DetailActivity.class));

and in DetailActivity

ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
if (extras.hasExtra("other")) {
    String other = (String) extras.getExtra("other");
}
like image 173
Milan Hlinák Avatar answered Nov 15 '22 18:11

Milan Hlinák


Checkout this post which says 1MB is a limit. Also checkout this one.

There is also a report on issues site.

The real question is: why pass a 1M data between activities? Perhaps a better way to achieve what you want is to persist this data and pass an identifier instead.

like image 27
amahfouz Avatar answered Nov 15 '22 19:11

amahfouz


Because startActivity will finally pass the whole Intent data to ActivityManagerService through Binder. And the Binder transaction buffer has a limited fixed size, currently 1Mb. Google Ref

like image 21
Cobain Avatar answered Nov 15 '22 20:11

Cobain