|
|
|
Android อยากทราบวิธีการจัดการ Memory โหลดรูปภาพจาก web server แล้วไม่ Out of memory |
|
|
|
|
|
|
|
ขอบคุณครับ ได้แล้วครับ
ไม่รู้ว่าจะ Out Of Memory อีกรึเปล่า
Code (Android-Java)
public static Bitmap loadBitmap(String url) {
bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new URL(url).openStream(),null,options);
//use your required height and width in place of 80 (size of imageview maybe)
options.inSampleSize = calculateInSampleSize(options,80, 80);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(new URL(url).openStream(),null,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
|
|
|
|
|
Date :
2013-05-02 15:45:39 |
By :
nutsai4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|