Skip to content

Add additional handling for low-memory devices #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/com/loopj/android/image/SmartImageTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public class SmartImageTask implements Runnable {
public static class OnCompleteHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap)msg.obj;
onComplete(bitmap);
try{
Bitmap bitmap = (Bitmap)msg.obj;
onComplete(bitmap);
}catch (OutOfMemoryError e){
e.printStackTrace();
}
}

public void onComplete(Bitmap bitmap){};
Expand Down Expand Up @@ -53,4 +57,4 @@ public void complete(Bitmap bitmap){
onCompleteHandler.sendMessage(onCompleteHandler.obtainMessage(BITMAP_READY, bitmap));
}
}
}
}
13 changes: 12 additions & 1 deletion src/com/loopj/android/image/WebImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.URL;
import java.net.URLConnection;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand Down Expand Up @@ -48,7 +49,17 @@ private Bitmap getBitmapFromUrl(String url) {
URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());

ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
int inSample = 1;
if(memInfo.lowMemory){
inSample = 12;
}

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSample;

bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent(), null, options);
} catch(Exception e) {
e.printStackTrace();
}
Expand Down