I try to download a image from web and decode it to bitmap. Sometimes it works but most the time app crashs. I know the problem with large images. But these images have only a max dimension of 300 pixel.
My testing device is Samsung S3.
new AsyncTask<Object, Void, Void>(){
@Override
protected Void doInBackground(Object[] params) {
try {
Connection connection = Jsoup.connect(".." + (String)params[1]);
Document doc = connection.timeout(30000).get();
String source = doc.select("..").attr("src");
source = source.matches("http(|s)://.+") ? source.replaceFirst("http(|s)://", "http://") : "http:" + source;
bitmap = BitmapFactory.decodeStream(new URL(source).openStream());
} catch (Exception e) {
Log.e(TAG, e);
}
return null;
}
}.execute(context, packageName, widgetId);
This code is in the onReceive Method of a AppWidgetProvider class. The widget runs till decoding Bitmap.
The error is different:
- Sometimes I get a OutOfMemoryException (on this time the occupied heap size is only 25-30m),
- sometimes the app does nothing (no error, IDE shows that is connected and in debug mode but no reaction anymore (mybe IntelliJ problem))
- sometimes the app doesnt crash but the bitmap is always null (most of time),
- and sometimes (rare) the app runs without any problems and the heap size grows with each loaded image till more than 60m (I dont test it longer)