解決Android解析圖片的OOM問(wèn)題的方法!!!
大家好,今天給大家分享的是解決解析圖片的出現(xiàn)oom的問(wèn)題,我們可以用BitmapFactory這里的各種Decode方法,如果圖片很小的話,不會(huì)出現(xiàn)oom,但是當(dāng)圖片很大的時(shí)候
就要用BitmapFactory.Options這個(gè)東東了,Options里主要有兩個(gè)參數(shù)比較重要.
options.inJustDecodeBounds = false/true; //圖片壓縮比例. options.inSampleSize = ssize;
我們?nèi)ソ馕鲆粋€(gè)圖片,如果太大,就會(huì)OOM,我們可以設(shè)置壓縮比例inSampleSize,但是這個(gè)壓縮比例設(shè)置多少就是個(gè)問(wèn)題,所以我們解析圖片可以分為倆個(gè)步驟,第一步就是
獲取圖片的寬高,這里要設(shè)置Options.inJustDecodeBounds=true,這時(shí)候decode的bitmap為null,只是把圖片的寬高放在Options里,然后第二步就是設(shè)置合適的壓縮比例inSampleSize,這時(shí)候獲得合適的Bitmap.這里我畫了簡(jiǎn)單的流程圖,如下:
為了讓大家更容易理解,我這里做了一個(gè)簡(jiǎn)單的demo,主要功能就是一個(gè)界面里有個(gè)ImageView,點(diǎn)擊ImageView的時(shí)候,進(jìn)入本地相冊(cè),選擇一個(gè)圖片的時(shí)候,ImageView控件顯示選擇的圖片。Demo的步驟如下:
第一步新建一個(gè)Android工程命名為ImageCacheDemo.目錄結(jié)構(gòu)如下:
第二步新建一個(gè)ImageCacheUtil.Java工具類,代碼如下:
package com.tutor.oom;
import java.io.InputStream;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
/**
* @author frankiewei.
* 工具類.
*/
public class ImageCacheUtil {
/**
* 獲取合適的Bitmap平時(shí)獲取Bitmap就用這個(gè)方法吧.
* @param path 路徑.
* @param data byte[]數(shù)組.
* @param context 上下文
* @param uri uri
* @param target 模板寬或者高的大小.
* @param width 是否是寬度
* @return
*/
public static Bitmap getResizedBitmap(String path, byte[] data,
Context context,Uri uri, int target, boolean width) {
Options options = null;
if (target > 0) {
Options info = new Options();
//這里設(shè)置true的時(shí)候,decode時(shí)候Bitmap返回的為空,
//將圖片寬高讀取放在Options里.
info.inJustDecodeBounds = false;
decode(path, data, context,uri, info);
int dim = info.outWidth;
if (!width)
dim = Math.max(dim, info.outHeight);
int ssize = sampleSize(dim, target);
options = new Options();
options.inSampleSize = ssize;
}
Bitmap bm = null;
try {
bm = decode(path, data, context,uri, options);
} catch(Exception e){
e.printStackTrace();
}
return bm;
}
/**
* 解析Bitmap的公用方法.
* @param path
* @param data
* @param context
* @param uri
* @param options
* @return
*/
public static Bitmap decode(String path, byte[] data, Context context,
Uri uri, BitmapFactory.Options options) {
Bitmap result = null;
if (path != null) {
result = BitmapFactory.decodeFile(path, options);
} else if (data != null) {
result = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
} else if (uri != null) {
//uri不為空的時(shí)候context也不要為空.
ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
try {
inputStream = cr.openInputStream(uri);
result = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 獲取合適的sampleSize.
* 這里就簡(jiǎn)單實(shí)現(xiàn)都是2的倍數(shù)啦.
* @param width
* @param target
* @return
*/
private static int sampleSize(int width, int target){
int result = 1;
for(int i = 0; i < 10; i++){
if(width < target * 2){
break;
}
width = width / 2;
result = result * 2;
}
return result;
}
}
第三步:修改ImageCacheDemoActivity.java代碼如下:
package com.tutor.oom;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
/**
* @author frankiewei.
* 解決圖片普通OOM的Demo.
*/
public class ImageCacheDemoActivity extends Activity {
/**
* 顯示圖片的ImageView.
*/
private ImageView mImageView;
/**
* 打開(kāi)本地相冊(cè)的requestcode.
*/
public static final int OPEN_PHOTO_REQUESTCODE = 0x1;
/**
* 圖片的target大小.
*/
private static final int target = 400;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews(){
mImageView = (ImageView)findViewById(R.id.imageview);
mImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
openPhotos();
}
});
}
/**
* 打開(kāi)本地相冊(cè).
*/
private void openPhotos() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, OPEN_PHOTO_REQUESTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case OPEN_PHOTO_REQUESTCODE:
if(resultCode == RESULT_OK){
//如果用這個(gè)方法,Options為null時(shí)候,就是默認(rèn)decode會(huì)出現(xiàn)oom哦.
//Bitmap bm = ImageCacheUtil.decode(null, null,
// ImageCacheDemoActivity.this, data.getData(), null);
//這里調(diào)用這個(gè)方法就不會(huì)oom.屌絲們就用這個(gè)方法吧.
Bitmap bm = ImageCacheUtil.getResizedBitmap(null, null,
ImageCacheDemoActivity.this, data.getData(), target, false);
mImageView.setImageBitmap(bm);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
其中main.xml布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageview"
android:layout_width="400px"
android:layout_height="400px"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
第四步運(yùn)行上述工程,效果如下:

從本地相冊(cè)選擇顯示。用了getRsizedBitmap()方法,圖片很大不會(huì)oom.
運(yùn)用默認(rèn)的decode方法就會(huì)oom。
OK,今天就講到這里,以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
原文鏈接:http://blog.csdn.net/android_tutor/article/details/8099918
相關(guān)文章
Android總結(jié)之WebView與Javascript交互(互相調(diào)用)
本篇文章主要介紹了WebView與Javascript進(jìn)行數(shù)據(jù)交互,詳解的講訴了WebView與Javascript進(jìn)行數(shù)據(jù)交互的方法,有興趣的可以了解一下。2016-11-11
Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果示例
這篇文章主要介紹了Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果,涉及Android結(jié)合Jni實(shí)現(xiàn)進(jìn)度條模擬壓力鍋數(shù)據(jù)監(jiān)測(cè)效果的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)Android?APP自動(dòng)更新功能
在移動(dòng)應(yīng)用的全生命周期中,版本迭代和用戶更新體驗(yàn)至關(guān)重要,傳統(tǒng)的做法是依賴?Google?Play?商店強(qiáng)制推送更新,但在某些場(chǎng)景下,我們需要更即時(shí)地控制更新流程,所以本文給大家介紹了Android實(shí)現(xiàn)Android?APP自動(dòng)更新功能,需要的朋友可以參考下2025-04-04
Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)教程
本文主要介紹了Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)的教程,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android Studio EditText點(diǎn)擊圖標(biāo)清除文本內(nèi)容的實(shí)例解析
這篇文章主要介紹了Android Studio EditText點(diǎn)擊圖標(biāo)清除文本內(nèi)容的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android實(shí)現(xiàn)列表數(shù)據(jù)按名稱排序、中英文混合排序
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)列表數(shù)據(jù)按名稱排序、中英文混合排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09

