if i want to get a large image (eg.a image which size is 2M or larger.)from url, i use the folllowing code ,and get the exception ."Out of Memory".
Any good idea?
Thank you.
private Bitmap getBitmapFromUrl(String url) {
Bitmap bitmap = null;
try {
URL imagePath = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imagePath.openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
InputStream is = (InputStream) conn.getContent();
if (is != null) {
// bitmap = getBitmapFromInputStream(is, width, height);
bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());
}
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;
}
public static Bitmap getBitmapFromInputStream(InputStream is, int width, int height){
Bitmap bitmap = null;
if (null != is ) {
BufferedInputStream bis = new BufferedInputStream(is);
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, opts);
// 计算图片缩放比例
final int minSideLength = Math.min(width, height);
opts.inSampleSize = BitmapUtils.computeSampleSize(opts.outWidth, opts.outHeight, minSideLength,
width * height);
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
opts.inPurgeable = true;
}
try {
bitmap = BitmapFactory.decodeStream(bis, null, opts);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
return bitmap;
}
杨辉
Impossible is nothing!
------------------------------------------------------------------------------------
HDExplorer APK (A Nice File Manager,Simplicity but not simple.)
Google Market:
https://market.android.com/details?id=com.hd.explorer
Google Code:
http://code.google.com/p/hdexplorer/
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment