Intents can only transmit small amounts of basic data. Transmission errors can occur when bitmaps are large.

Intent to deliver big data, there will be a TransactionTooLargeException scenario, in TransactionTooLargeException (developer.android.com/reference/a…). In fact, the trigger cause has been specified in the documentation. Question:

Intents use binders to transmit data. The data in the Intent is transmitted as objects stored in the Binder transaction buffer for the Parcel. The Binder transaction buffer has a finite fixed size, currently 1MB. Don’t assume that passing less than 1MB of data is safe; the 1MB space is not exclusive to the current operation, but is shared by the current process. In other words, intEnts transfer data between activities and are not suitable for transferring too much data by themselves. Detail Note 1:

Intents use bundles to store data. Are they passed by value (deep copy) or by reference? Intent data is stored in an mExtras object. The Bundle requires that all data stored in the mExtras object be serializable. 2. In Android, serialization data needs to be either Serializable or Parcelable. The wrapper class for the underlying data type itself implements Serializable, while our custom object implements either of the two serialization interfaces on demand. Detail Note 2: Does serialization occur whenever data is passed through the Bundle?

No, cross-process communication is a primary concern between activities, and Binder mechanisms handle cross-process communication in Android. When it comes to cross-processes, serialization and deserialization are involved for complex data, which is bound to be a value transfer (deep copy) process. This problem using reduction to absurdity can also explain, if is the reference, the transfer of the past only object reference, points to the object store address, only equivalent to the size of an Int, also won’t appear abnormal TransactionTooLargeException. Detail 3: Transferring data serialization has nothing to do with Bundle, only with cross-process communication with Binder.

In Android, the use of bundles to transfer data is not unique to intEnts. For example, with popovers, you can also pass a Bundle object to the dialog box in DialogFragment using setArguments(Bundle). The Fragment itself is cross-process free. The Bundle is used to transfer data, but not through Binder, meaning there is no serialization or deserialization. The Bundle associated with Fragment data passing is actually passing a reference to the original object. How to solve:

Since Binder transmission limits the size of data, we can communicate without Binder. Consider it from the data source:

Bitmap, for example, already implements Parcelable to support serialization. With Intent to transmit, a little bit big figure will appear TransactionTooLargeException. Of course, in a business scenario, there is no Bitmap transfer. So let’s look at the data source for this image. Drawable? Local file? Online pictures? No matter where the data source is, we just need to pass a Drawable_ID, path, URL, and restore the image without passing the Bitmap object. Big data always has a data source, and restoring data from a data source is just calling a method for us. From the Android Developer’s Manual the first: Use Intent and deliver it through parcelable

// New Intent = new Intent(this, Two. Class); // New Intent(this, Two. intent.putExtra(“key”, bitmap);

Intent Intent = new Intent(); // New Intent(); // New Intent(); Bitmap bitmap = (Bitmap) intent.getParcelableExtra(“key”); Note: In the second activity, the Bitmap must be strong or the second activity cannot be delivered with an Intent that is passed through a byte array

// New Intent = new Intent(this,Two. Class); // New Intent(this,Two. ByteArrayOutputStream bs = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs); byte[] bitmapByte = bs.toByteArray(); intent.putExtra(“bp”, bitmapByte); this.startActivity(intent);

// ActivityB [] byteTemp = intent. GetByteArrayExtra (“bitmap”); bitmap = BitmapFactory.decodeByteArray(byteTemp, 0, byteTemp.length); 1 2 3 4 5 6 7 8 9 10 11 Third: write to a file as a stream, and pass the file path to another Activity through the Intent

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.os.Environment;

Public class BitmapUtil {/** * save Bitmap as file; * * @param BMP * @param filename * @return */ public static void saveBitmap2file String filename){ CompressFormat format = Bitmap.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try{ stream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + “/” + filename + “.jpg”); } catch (FileNotFoundException e){ e.printStackTrace(); } bmp.compress(format, quality, stream); try{ stream.flush(); stream.close(); } catch (IOException e){ e.printStackTrace(); }}

/** * Read the file as Bitmap ** @param filename * @return * @throws FileNotFoundException */Copy the code

public static Bitmap getBitmapFromFile(String filename){ try{ return BitmapFactory.decodeStream(newFileInputStream(Environment.getExternalStorageDirectory().getPath() + “/” + filename + “.jpg”)); } catch (FileNotFoundException e){ e.printStackTrace(); } return null; }

} — — — — — — — —

Reference: blog.csdn.net/weixin_4472…