国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android獲取網(wǎng)絡(luò)圖片內(nèi)存溢出的方法

2020-02-21 17:35:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在Android軟件開(kāi)發(fā)過(guò)程中,程序員會(huì)經(jīng)常遇到圖像處理的問(wèn)題,當(dāng)遇到大圖片時(shí)都會(huì)出現(xiàn)內(nèi)存超出的問(wèn)題,下面就讓武林技術(shù)頻道小編帶大家來(lái)學(xué)習(xí)Android獲取網(wǎng)絡(luò)圖片內(nèi)存溢出的方法吧!

Android獲取網(wǎng)絡(luò)圖片內(nèi)存溢出的方法
1.AndroidManifest.xml 權(quán)限配置
添加互聯(lián)網(wǎng)訪問(wèn)權(quán)限:

復(fù)制代碼 代碼如下:


2.異步圖片類 ImageDownloadTask

?

復(fù)制代碼 代碼如下:

?


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class ImageDownloadTask extends AsyncTask {
private ImageView imageView = null;
/***
* 這里獲取到手機(jī)的分辨率大小
* */
public void setDisplayWidth(int width) {
_displaywidth = width;
}
public int getDisplayWidth() {
return _displaywidth;
}
public void setDisplayHeight(int height) {
_displayheight = height;
}
public int getDisplayHeight() {
return _displayheight;
}
public int getDisplayPixels() {
return _displaypixels;
}
private int _displaywidth = 480;
private int _displayheight = 800;
private int _displaypixels = _displaywidth * _displayheight;
@Override
protected Bitmap doInBackground(Object... params) {
// TODO Auto-generated method stub
Bitmap bmp = null;
imageView = (ImageView) params[1];
try {
String url = (String) params[0];
bmp = getBitmap(url, _displaypixels,true);
} catch (Exception e) {
return null;
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
if (imageView != null&&result!=null) {
imageView.setImageBitmap(result);
if (null != result && result.isRecycled() == false)
System.gc();
}
}
/**
* 通過(guò)URL獲得網(wǎng)上圖片。如:http://www.xxxxxx.com/xx.jpg
* */
public Bitmap getBitmap(String url, int displaypixels, Boolean isBig) throws MalformedURLException, IOException {
Bitmap bmp = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
InputStream stream = new URL(url).openStream();
byte[] bytes = getBytes(stream);
//這3句是處理圖片溢出的begin( 如果不需要處理溢出直接 opts.inSampleSize=1;)
opts.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
opts.inSampleSize = computeSampleSize(opts, -1, displaypixels);
//end
opts.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
return bmp;
}
/**
* 數(shù)據(jù)流轉(zhuǎn)成btyle[]數(shù)組
* */
private byte[] getBytes(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[2048];
int len = 0;
try {
while ((len = is.read(b, 0, 2048)) != -1) {
baos.write(b, 0, len);
baos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = baos.toByteArray();
return bytes;
}
/****
* 處理圖片bitmap size exceeds VM budget (Out Of Memory 內(nèi)存溢出)
*/
private int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
,>


3.測(cè)試調(diào)用代碼

?

復(fù)制代碼 代碼如下:

?


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageDownloadTask imgtask =new ImageDownloadTask();
/**這里是獲取手機(jī)屏幕的分辨率用來(lái)處理 圖片 溢出問(wèn)題的。begin*/
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imgtask.setDisplayWidth(dm.widthPixels);
imgtask.setDisplayHeight(dm.heightPixels);
//end
ImageView imageView_test= (ImageView)findViewById(R.id.imageView_test);
imgtask.execute("http://pic.qukantu.com/big/7515/201201031116491.jpg",imageView_test);
}

上文是Android獲取網(wǎng)絡(luò)圖片內(nèi)存溢出的方法,相信大家都有了一定的了解,想要了解更多的技術(shù)信息,請(qǐng)繼續(xù)關(guān)注武林技術(shù)頻道吧!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄梅县| 山西省| 镇平县| 马边| 西城区| 丹巴县| 潼关县| 马关县| 巴里| 黄平县| 洪泽县| 定兴县| 桂东县| 潍坊市| 桓台县| 永安市| 和静县| 大荔县| 深圳市| 富阳市| 临城县| 称多县| 崇仁县| 鄂托克前旗| 克什克腾旗| 云梦县| 洛川县| 普宁市| 阜康市| 华宁县| 河北省| 厦门市| 定安县| 偃师市| 武义县| 武威市| 铜梁县| 巴彦县| 海原县| 历史| 土默特右旗|