I. Image OOM exception: Sample Code:
public class MainActivity extends Activity {private ImageView iv_imageView;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_imageView = (ImageView) findViewById(R.id.Iv_imageView);}public void load(View view){Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/1.jpg");iv_imageView.setImageBitmap(bitmap);}}
Problem:
5038848,
20155392 Byte ---> 20155392/1024 = 19683KB ---> 19638/1024 = 19.2216796875MB19.2216796875MB
6.
Ii. Image Scaling Algorithm:
1.
2.
Code details:
Public class MainActivity extends Activity {private ImageView iv_imageView; private int running wwidth; private int running wheight; @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv_imageView = (ImageView) findViewById (R. id. iv_imageView); // 1. get the resolution width and height of the mobile phone screen Display = getWindowManager (). getdefadisplay display (); required wwidth = display. getWidth (); returns wheight = display. getHeight (); System. out. println ("screen width:" + shadowwheight); System. out. println ("screen height:" + windowHeight);} public void load (View view) {// Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/1.jpg"); // iv_imageView.setImageBitmap (bitmap); Options opts = new Options (); // 2.0 sets the parser not to parse the bitmap, instead, it parses the out output (width, height) of the image and does not apply for an internal opts space for each vertex of the image. inJustDecodeBounds = true; // 2. obtain the image width and height information BitmapFactory. decodeFile ("/sdcard/1.jpg", opts); int picHeight = opts. outHeight; int picWidth = opts. outWidth; System. out. println ("Image Width:" + picWidth); System. out. println ("Image Height:" + picHeight); // 3. int scaleX = picWidth/20.wwidth; int scaleY = picHeight/windowHeight; System. out. println ("horizontal scaling:" + scaleX); System. out. println ("vertical scaling ratio:" + scaleY); // 3.2. determine the scaling ratio: int scale = 1; if (scaleX> scaleY & scaleY> 1) {scale = scaleX;} if (scaleY> scaleX & scaleX> 1) {scale = scaleY;} System. out. println ("final scaled ratio:" + scale); // 4. actually parse this image opts. inJustDecodeBounds = false; opts. inSampleSize = scale; Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/1.jpg", opts); iv_imageView.setImageBitmap (bitmap );}}
Runtime log:
09-29 16:05:37. 715: I/System. out (23786 ):
09-29 16:05:37. 715: I/System. out (23786 ):
09-29 16:07:12. 085: I/System. out (23786 ):
09-29 16:07:12. 085: I/System. out (23786 ):
09-29 16:07:12. 105: I/System. out (23786 ):
09-29 16:07:12. 115: I/System. out (23786 ):
09-29 16:07:12. 115: I/System. out (23786 ):
09-29 16:17:26. 475: I/System. out (23786 ):
09-29 16:17:26. 475: I/System. out (23786 ):
09-29 16:17:33. 336: I/System. out (23786 ):
09-29 16:17:33. 336: I/System. out (23786 ):
09-29 16:17:33. 345: I/System. out (23786 ):
09-29 16:17:33. 345: I/System. out (23786 ):
09-29 16:17:33. 345: I/System. out (23786 ):