Android中怎么利用OOM解决异常

发布时间:2021-08-03 10:42:58 作者:Leah
来源:亿速云 阅读:165

本篇文章为大家展示了Android中怎么利用OOM解决异常,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

堆内存空间主要是给类实例、数组分配空间。当图片占用的空间大于对内存空间时就会抛出内存溢出的异常。

本示例是在加载15M左右的图片而引起的OOM异常,默认情况下,虚拟机只语序允许加载10M以内大小的图片。如果超过10M,则会抛出OOM异常

问题解决思路:缩放加载图片

1、得到设备屏幕的分辨率:
2、得到原图的分辨率:
3、通过比较得到一个合适的比例值:
4、使用比例值缩放一张图片,并加载到内存中:

示例代码:

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.获取手机屏幕分辨率的大小
    	WindowManager wm=(WindowManager) getSystemService(WINDOW_SERVICE);
    	Display display = wm.getDefaultDisplay();
    	int screenHeight = display.getHeight();
    	int screenWidth = display.getWidth();
    	//2.获取原图分辨率的大小
    	Options opts=new Options();
    	opts.inJustDecodeBounds=true;
		BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/1.bmp", opts);
    	int outHeight = opts.outHeight;
    	int outWidth = opts.outWidth;
    	//3.得到缩放比
    	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.使用比例值缩放一张图片,并加载到内存中:
    	opts.inJustDecodeBounds=false;
    	opts.inSampleSize=scale;
    	Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/1.bmp", opts);
    	
    	iv.setImageBitmap(bitmap);
	}
}

布局文件代码:

<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="加载大图片"
	    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>

上述内容就是Android中怎么利用OOM解决异常,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 【移动开发】Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法
  2. Keras 如何快速解决OOM超内存?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android oom

上一篇:C++中怎么利用LeetCode实现二叉搜索树迭代器

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》