What to do on TransactionTooLargeException

前端 未结 30 3390
盖世英雄少女心
盖世英雄少女心 2020-11-22 03:08

I got a TransactionTooLargeException. Not reproducible. In the docs it says

The Binder transaction failed because it was too large.

D

30条回答
  •  鱼传尺愫
    2020-11-22 03:47

    Issue is resolved by:

     Bundle bundle = new Bundle();
      bundle.putSerializable("data", bigdata);
    ...
      CacheHelper.saveState(bundle,"DATA");
      Intent intent = new Intent(mActivity, AActivity.class);
      startActivity(intent, bb);// do not put data to intent.
    
    In Activity:
       @Override
       protected void onCreate(Bundle savedInstanceState) {
            Bundle bundle = CacheHelper.getInstance().loadState(Constants.DATA);
            if (bundle != null){
                Intent intent = getIntent();
                intent.putExtras(bundle);
            }
            getIntent().getExtra(..);
            ....
       }
       @Override
        protected void onDestroy() {
            super.onDestroy();
            CacheHelper.clearState("DATA");
        }
    
    public class CacheHelper {
    
        public static void saveState(Bundle savedInstanceState, String name) {
            Bundle saved = (Bundle) savedInstanceState.clone();
            save(name, saved);
        }
        public Bundle loadState(String name) {
    
            Object object = load(name);
            if (object != null) {
                Bundle bundle = (Bundle) object;
                return bundle;
            }
            return null;
        }
        private static void save(String fileName, Bundle object) {
            try {
                String path = StorageUtils.getFullPath(fileName);
                File file = new File(path);
                if (file.exists()) {
                    file.delete();
                }
                FileOutputStream fos = new FileOutputStream(path, false);
    
                Parcel p = Parcel.obtain(); //creating empty parcel object
                object.writeToParcel(p, 0); //saving bundle as parcel
                //parcel.writeBundle(bundle);
                fos.write(p.marshall()); //writing parcel to file
    
                fos.flush();
                fos.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        private static Bundle load(String fileName) {
            try {
                String path = StorageUtils.getFullPath(fileName);
                FileInputStream fis = new FileInputStream(path);
    
                byte[] array = new byte[(int) fis.getChannel().size()];
                fis.read(array, 0, array.length);
    
                Parcel parcel = Parcel.obtain(); //creating empty parcel object
                parcel.unmarshall(array, 0, array.length);
                parcel.setDataPosition(0);
                Bundle out = parcel.readBundle();
                out.putAll(out);
    
                fis.close();
                parcel.recycle();
                return out;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    public static void clearState(Activity ac) {
        String name = ac.getClass().getName();
        String path = StorageUtils.getFullPath(name);
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }
    }
    

提交回复
热议问题