溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中怎么利用OOM解決異常

發(fā)布時間:2021-08-03 10:42:58 來源:億速云 閱讀:177 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了Android中怎么利用OOM解決異常,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

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

堆內(nèi)存空間主要是給類實例、數(shù)組分配空間。當(dāng)圖片占用的空間大于對內(nèi)存空間時就會拋出內(nèi)存溢出的異常。

本示例是在加載15M左右的圖片而引起的OOM異常,默認(rèn)情況下,虛擬機(jī)只語序允許加載10M以內(nèi)大小的圖片。如果超過10M,則會拋出OOM異常

問題解決思路:縮放加載圖片

1、得到設(shè)備屏幕的分辨率:
2、得到原圖的分辨率:
3、通過比較得到一個合適的比例值:
4、使用比例值縮放一張圖片,并加載到內(nèi)存中:

示例代碼:

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.獲取手機(jī)屏幕分辨率的大小
    	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.使用比例值縮放一張圖片,并加載到內(nèi)存中:
    	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>

上述內(nèi)容就是Android中怎么利用OOM解決異常,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI