Explanation of OOM Exception Resolution Case of Android

  • 2021-12-13 09:47:50
  • OfStack


02-03 08:56:12.411: E/AndroidRuntime(10137): FATAL EXCEPTION: main
02-03 08:56:12.411: E/AndroidRuntime(10137): java.lang.IllegalStateException: Could not execute method of the activity
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.view.View$1.onClick(View.java:3591)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.view.View.performClick(View.java:4084)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.view.View$PerformClick.run(View.java:16966)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.os.Handler.handleCallback(Handler.java:615)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.os.Looper.loop(Looper.java:137)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at java.lang.reflect.Method.invoke(Method.java:511)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at dalvik.system.NativeStart.main(Native Method)
02-03 08:56:12.411: E/AndroidRuntime(10137): Caused by: java.lang.reflect.InvocationTargetException
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at java.lang.reflect.Method.invoke(Method.java:511)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.view.View$1.onClick(View.java:3586)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	... 11 more
02-03 08:56:12.411: E/AndroidRuntime(10137): Caused by: java.lang.OutOfMemoryError
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:326)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	at com.ithema.bitmap.MainActivity.down(MainActivity.java:52)
02-03 08:56:12.411: E/AndroidRuntime(10137): 	... 14 more

Heap memory space is mainly allocated to class instances and arrays. When the space occupied by the picture is larger than the memory space, an exception of memory overflow will be thrown.

This example is an OOM exception caused by loading pictures around 15M. By default, virtual machines only allow loading pictures within 10M. If it exceeds 10M, an OOM exception will be thrown

Problem Solution: Zoom and Load Pictures

1. Get the resolution of the device screen:
2. Get the resolution of the original image:
3. A suitable proportion value is obtained by comparison:
4. Scale a picture with a scale value and load it into memory:

Sample code:


package com.ithema.bitmap;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
    private ImageView iv;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }
    public void down(View view) {
		//1. Get the size of the screen resolution of the mobile phone 
    	WindowManager wm=(WindowManager) getSystemService(WINDOW_SERVICE);
    	Display display = wm.getDefaultDisplay();
    	int screenHeight = display.getHeight();
    	int screenWidth = display.getWidth();
    	//2. Gets the size of the original resolution 
    	Options opts=new Options();
    	opts.inJustDecodeBounds=true;
		BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/1.bmp", opts);
    	int outHeight = opts.outHeight;
    	int outWidth = opts.outWidth;
    	//3. Get the scaling ratio 
    	int scale=1;
    	int scaleX=outWidth/screenWidth;
    	int scaleY=outHeight/screenHeight;
    	if(scaleX>scaleY&&scaleX>1){
    		scale=scaleX;
    	}
    	if(scaleY>scaleX&&scaleY>1){
    		scale=scaleY;
    	}
    	
    	//4. Scaling with a scale value 1 Picture and load it into memory: 
    	opts.inJustDecodeBounds=false;
    	opts.inSampleSize=scale;
    	Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/1.bmp", opts);
    	
    	iv.setImageBitmap(bitmap);
	}
}

Layout file code:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
	<Button 
	    android:onClick="down"
	    android:text=" Load a large picture "
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
	    />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
 
</LinearLayout>

Related articles: