I want go get image from URL and convert it into drawable. I have this method, that works fine:
public static Drawable getDrawableFromUrl(String url) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x);
}
Except one thing. When image is too big app crashes.
E/dalvikvm-heap(12750): 13142360-byte external allocation too large for this process.
E/dalvikvm(12750): Out of memory: Heap Size=6023KB, Allocated=3177KB, Bitmap Size=2563KB, Limit=20480KB
E/dalvikvm(12750): Trim info: Footprint=6023KB, Allowed Footprint=6023KB, Trimmed=952KB
E/GraphicsJNI(12750): VM won't let us allocate 13142360 bytes
Can I somehow increase the memory that I need for the image? Or maybe there is a way around this problem?
Ok. Here is the code that finaly works.
As suggested I reduced the size of image using given function: calculateInSampleSize – thx Androyds.