有人通过intent传输数据,结果发现数据量有几M,出现问题:
D/dalvikvm( 5731): WAIT_FOR_CONCURRENT_GC blocked 0ms
E/JavaBinder( 641): !!! FAILED BINDER TRANSACTION !!!
W/ActivityManager( 641): Exception in new application when starting activity xxxActivity
W/ActivityManager( 641): android.os.TransactionTooLargeException
W/ActivityManager( 641): at android.os.BinderProxy.transact(Native Method)
W/ActivityManager( 641): at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:705)
W/ActivityManager( 641): at com.android.server.am.ActivityStack.realStartActivityLocked(ActivityStack.java:693)
W/ActivityManager( 641): at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:4182)
W/ActivityManager( 641): at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:4260)
W/ActivityManager( 641): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:357)
W/ActivityManager( 641): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1672)
W/ActivityManager( 641): at android.os.Binder.execTransact(Binder.java:367)
W/ActivityManager( 641): at com.android.server.SystemServer.init1(Native Method)
W/ActivityManager( 641): at com.android.server.SystemServer.main(SystemServer.java:1007)
W/ActivityManager( 641): at java.lang.reflect.Method.invokeNative(Native Method)
W/ActivityManager( 641): at java.lang.reflect.Method.invoke(Method.java:511)
W/ActivityManager( 641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
W/ActivityManager( 641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
W/ActivityManager( 641): at dalvik.system.NativeStart.main(Native Method)
D/ActivityThread( 5745): setTargetHeapUtilization:0.25
D/ActivityThread( 5745): setTargetHeapIdealFree:8388608
D/ActivityThread( 5745): setTargetHeapConcurrentStart:2097152
通过google搜索stackoverflow,发现:
As per my experience (sometime ago), you are able to put up to 1MB of data in a Bundle
encapsulated inside Intent
. I think, this restriction was valid up till Froyo or GingerBread.
However, in order to overcome this issue, I would suggest you to save your content on a temp file and pass the path/URI of your temp file to your second activity. Then in your second activity, read the contents out from file, perform your desired operation and finally delete that file.
If you want, you may also incorporate Shared_Preferences for this task - if you think handling files is cumbersome.
The size limit of Intent is still pretty low in Jelly Bean, which is somewhat lower than 1MB (around 90K), so you should always be cautious about your data length, even if your application targets only latest Android versions.
那么通过intent如何传输大数据呢?压缩数据,不过数据量也不能太大,目前我们的测试数据是8M字节左右。如果大于这个数据,需要通过其他手段传输,例如文件或者共享内存。