欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android之OOM異常解決案例講解

 更新時(shí)間:2021年08月03日 08:51:08   作者:懸弧  
這篇文章主要介紹了Android之OOM異常解決案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
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í)例、數(shù)組分配空間。當(dāng)圖片占用的空間大于對內(nèi)存空間時(shí)就會拋出內(nèi)存溢出的異常。

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

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

1、得到設(shè)備屏幕的分辨率:
2、得到原圖的分辨率:
3、通過比較得到一個(gè)合適的比例值:
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>

到此這篇關(guān)于Android之OOM異常解決案例講解的文章就介紹到這了,更多相關(guān)Android之OOM異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android開發(fā)教程之shape和selector的結(jié)合使用

    Android開發(fā)教程之shape和selector的結(jié)合使用

    shape和selector是Android UI設(shè)計(jì)中經(jīng)常用到的,比如我們要自定義一個(gè)圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector,接下來通過本文給大家介紹Android開發(fā)教程之shape和selector的結(jié)合使用,感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android車載空調(diào)系統(tǒng)(HVAC)開發(fā)方法分析

    Android車載空調(diào)系統(tǒng)(HVAC)開發(fā)方法分析

    HVAC?全稱:供暖通風(fēng)與空氣調(diào)節(jié)(Heating?Ventilation?and?Air?Conditioning),用戶可以通過他來控制整個(gè)汽車的空調(diào)系統(tǒng),是汽車中非常重要的一個(gè)功能,汽車的空調(diào)HMI雖然并不復(fù)雜,但是大多都是用符號來表示功能,必須理解空調(diào)的各個(gè)符號表示的含義
    2023-12-12
  • Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼

    Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼

    這篇文章主要介紹了Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Android自定義進(jìn)度條效果

    Android自定義進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android GridView仿微信朋友圈顯示圖片

    Android GridView仿微信朋友圈顯示圖片

    這篇文章主要介紹了Android GridView仿微信朋友圈顯示圖片,上傳多圖并且多圖顯示,GridView可以動態(tài)加載圖片的數(shù)量,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android最新狀態(tài)欄處理介紹

    Android最新狀態(tài)欄處理介紹

    大家好,本篇文章主要講的是Android最新狀態(tài)欄處理介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • android 獲取APP的唯一標(biāo)識applicationId的實(shí)例

    android 獲取APP的唯一標(biāo)識applicationId的實(shí)例

    下面小編就為大家分享一篇android 獲取APP的唯一標(biāo)識applicationId的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法

    android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法

    以前倒是沒有注意listview的getView會重復(fù)執(zhí)行多次,在測試的時(shí)候去斷點(diǎn)跟蹤,發(fā)現(xiàn)同一條數(shù)據(jù)不斷的重復(fù)執(zhí)行,下面與大家分享下正確的解決方法,希望對你有所幫助
    2013-06-06
  • Android實(shí)現(xiàn)掃描二維碼功能

    Android實(shí)現(xiàn)掃描二維碼功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)掃描二維碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Android使用開源組件PagerBottomTabStrip實(shí)現(xiàn)底部菜單和頂部導(dǎo)航功能

    Android使用開源組件PagerBottomTabStrip實(shí)現(xiàn)底部菜單和頂部導(dǎo)航功能

    這篇文章主要介紹了Android使用PagerBottomTabStrip實(shí)現(xiàn)底部菜單和頂部導(dǎo)航功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-08-08

最新評論