In Android applications, image resources are the most memory-consuming resources. In addition, in the Android system, when reading Bitmap, the size of the image stack assigned to the virtual machine is only 8 Mb. If the size exceeds, an OutOfMemory exception occurs.
 
E/AndroidRuntime (697): java. lang. OutOfMemoryError
E/AndroidRuntime (697): at android. graphics. BitmapFactory. nativeDecodeAsset (Native Method)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeStream (BitmapFactory. java: 500)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResourceStream (BitmapFactory. java: 353)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResource (BitmapFactory. java: 376)
E/AndroidRuntime (697): at android. graphics. BitmapFactory. decodeResource (BitmapFactory. java: 406)
E/AndroidRuntime (697): at com. example. imagetoshow2.ImageAdapter. createReflectedImages (ImageAdapter. java: 66)
E/AndroidRuntime (697): at com. example. imagetoshow2.ImageAdapter. getView (ImageAdapter. java: 54)
E/AndroidRuntime (697): at android. widget. AbsSpinner. onMeasure (AbsSpinner. java: 193)
Solution:
 
1. Reclaim memory in time
 
If (bitmap! = Null &&! Bitmap. isRecycled () {// recycle and set it to null bitmap. recycle (); bitmap = null;} System. gc ();Use the above Code in a proper place to recycle unused Code temporarily. Of course, system. gc should not be called frequently; otherwise, the system efficiency will be reduced. 
 
 
2. Use BitmapFactory. Options to compress the image
 
 
 BitmapFactory.Options opts = new BitmapFactory.Options();   opts.inSampleSize = n;           bitmap = BitmapFactory.decodeStream(fis, null, opts);
 
Use inSampleSize to set the scale-down ratio. The default value is 0. Set a number greater than 0 to compress the image. 
 
 
BitmapFactory. options opts = new BitmapFactory. options (); // set inJustDecodeBounds to true opts. inJustDecodeBounds = true; // use the decodeFile method to obtain the image width and height BitmapFactory. decodeFile (path, opts );
 
After inJustDecodeBounds is set to true, decodeFile () and other methods are used, and no space is actually allocated. That is, the decoded Bitmap is null and only options is calculated. outWidth and options. outHeight value. Before instantiating a Bitmap object by using the decodeFile () method of BitmapFactory. set inJustDecodeBound to false to get the image. 
 
3. code optimization
 
To avoid an OutOfMemory exception that occurs when an application allocates Bitmap memory, an OutOfMemory exception is often caught in the Code for instantiating Bitmap.
 
<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> Bitmap bitmap = null; try {// instantiate Bitmap bitmap = BitmapFactory. decodeFile (path);} catch (OutOfMemoryError e) {//} </span>Then perform some memory reclaim operations in the Catch part, or use cached images... 
Always a good programming style and high-quality code structure are the ultimate pursuit of programmers ....