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
堆內(nèi)存空間主要是給類實例、數(shù)組分配空間。當圖片占用的空間大于對內(nèi)存空間時就會拋出內(nèi)存溢出的異常。
本示例是在加載15M左右的圖片而引起的OOM異常,默認情況下,虛擬機只語序允許加載10M以內(nèi)大小的圖片。如果超過10M,則會拋出OOM異常
問題解決思路:縮放加載圖片
1、得到設備屏幕的分辨率:
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.獲取手機屏幕分辨率的大小
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>
到此這篇關于Android之OOM異常解決案例講解的文章就介紹到這了,更多相關Android之OOM異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android開發(fā)手冊TextView屬性實現(xiàn)效果盤點
這篇文章主要為大家介紹了Android開發(fā)手冊TextView屬性實現(xiàn)的效果盤點及使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Android跳轉到系統(tǒng)聯(lián)系人及撥號或短信界面
現(xiàn)在開發(fā)中的功能需要直接跳轉到撥號、聯(lián)系人、短信界面等等,查找了很多資料,自己整理了一下特此分享到腳本之家平臺供大家參考2016-12-12
解決Android 10/Android Q手機在后臺無法正常定位問題
這篇文章主要介紹了解決Android 10/Android Q手機在后臺無法正常定位問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Android利用Hero實現(xiàn)列表與詳情頁無縫切換動畫
本文我們將利用Hero動畫實現(xiàn)一個簡單案例:實現(xiàn)列表與詳情頁無縫切換動畫,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下2022-06-06
一文詳解Android無需權限調(diào)用系統(tǒng)相機拍照
這篇文章主要為大家介紹了Android無需權限調(diào)用系統(tǒng)相機拍照詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
MT6589平臺通話錄音時播放提示音給對方功能的具體實現(xiàn)
MT6589平臺通話錄音時如何播放提示音給對方,可以通過修改以下文件即可,希望對你有所幫助2013-06-06
最好用的Android省市區(qū)三級聯(lián)動選擇效果
這篇文章主要為大家詳細介紹了最好用的Android省市區(qū)三級聯(lián)動選擇效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

