02-03 08:56:12.411: E/AndroidRuntime(10137): FATAL EXCEPTION: main 02-03 08:56:12.411: E/AndroidRuntime(10137): : Could not execute method of the activity 02-03 08:56:12.411: E/AndroidRuntime(10137): at $(:3591) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:4084) 02-03 08:56:12.411: E/AndroidRuntime(10137): at $(:16966) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:615) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:92) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:137) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:4745) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (Native Method) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:511) 02-03 08:56:12.411: E/AndroidRuntime(10137): at $(:786) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:553) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (Native Method) 02-03 08:56:12.411: E/AndroidRuntime(10137): Caused by: 02-03 08:56:12.411: E/AndroidRuntime(10137): at (Native Method) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:511) 02-03 08:56:12.411: E/AndroidRuntime(10137): at $(:3586) 02-03 08:56:12.411: E/AndroidRuntime(10137): ... 11 more 02-03 08:56:12.411: E/AndroidRuntime(10137): Caused by: 02-03 08:56:12.411: E/AndroidRuntime(10137): at (Native Method) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:527) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:301) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:326) 02-03 08:56:12.411: E/AndroidRuntime(10137): at (:52) 02-03 08:56:12.411: E/AndroidRuntime(10137): ... 14 more
The heap memory space mainly allocates space to class instances and arrays. When the space occupied by the image is greater than the memory space, a memory overflow exception will be thrown.
This example is an OOM exception caused by loading pictures about 15M. By default, the virtual machine only allows loading pictures of sizes within 10M. If it exceeds 10M, an OOM exception will be thrown
Problem solution idea: zoom loading pictures
1. Get the resolution of the device screen:
2. Get the resolution of the original image:
3. Get an appropriate proportional value through comparison:
4. Use the scale value to scale a picture and load it into memory:
Sample code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); iv = (ImageView) findViewById(); } public void down(View view) { //1. Get the resolution of the mobile phone screen WindowManager wm=(WindowManager) getSystemService(WINDOW_SERVICE); Display display = (); int screenHeight = (); int screenWidth = (); //2. Get the resolution of the original image Options opts=new Options(); =true; (()+"/", opts); int outHeight = ; int 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. Scale a picture with scale values and load it into memory: =false; =scale; Bitmap bitmap = (()+"/", opts); (bitmap); } }
Layout file code:
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:onClick="down" android:text="Loading large pictures" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android: android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
This is the article about the case of solving the OOM exception of Android. This is all about this article. For more related content on OOM exceptions of Android, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!