java - android download Bitmap from url ran into out of memory exception -
i`m building app loads images url , when user scroll down app download more images.
after 30 downloads out of memory exception. read here , in android developer not find way make run.
this code download image:
for (int k = 0; k < pictureary.length(); k++) { try { url url = new url(picurl + pictureary.getstring(k) + "/230"); httpurlconnection connection = (httpurlconnection) url .openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmap mybitmap = bitmapfactory.decodestream(input); picarraylist.add(mybitmap); break; } catch (ioexception e) { bitmap mybitmap = bitmapfactory.decoderesource( context.getresources(), r.drawable.icon_0404); picarraylist.add(mybitmap); } }
and 1 display:
enter codeint bwidth = picarraylist.get(0).getwidth(); int bheight = picarraylist.get(0).getheight(); int swidth = maintableforallposts.getwidth(); int sheight = maintableforallposts.getheight(); int new_width = swidth; int new_height = (int) math.floor((double) bheight * ((double) new_width / (double) bwidth)); newbitmap = bitmap.createscaledbitmap( picarraylist.get(0), new_width, new_height, true); mainimageview.setimagebitmap(newbitmap); } else mainimageview .setimagebitmap(overlay(bitmapforvideothumbnails)); } catch (exception e) { mainimageview.setimageresource(com.gy.bll.r.drawable.icon_0404); }
its not writing here try use bitmapfactory.options got stack because source url , not file.
this log cat:
06-27 12:30:10.008: e/dalvikvm(11265): out of memory: heap size=98304kb, allocated=92688kb, limit=98304kb, proc limit=98304kb 06-27 12:30:10.008: e/dalvikvm(11265): info: footprint=97504kb, allowed footprint=98304kb, trimmed=1588kb 06-27 12:30:10.013: e/bitmap_jni(11265): create bitmap failed. 06-27 12:30:10.013: e/bitmap_jni(11265): failed create skbitmap! 06-27 12:30:10.013: w/dalvikvm(11265): threadid=16: thread exiting uncaught exception (group=0x41991a08) 06-27 12:30:10.013: e/androidruntime(11265): fatal exception: asynctask #5 06-27 12:30:10.013: e/androidruntime(11265): java.lang.runtimeexception: error occured while executing doinbackground() 06-27 12:30:10.013: e/androidruntime(11265): @ android.os.asynctask$3.done(asynctask.java:299) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.futuretask.run(futuretask.java:239) 06-27 12:30:10.013: e/androidruntime(11265): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 06-27 12:30:10.013: e/androidruntime(11265): @ java.lang.thread.run(thread.java:864) 06-27 12:30:10.013: e/androidruntime(11265): caused by: java.lang.outofmemoryerror: (heap size=98304kb, allocated=92688kb) 06-27 12:30:10.013: e/androidruntime(11265): @ android.graphics.bitmap.nativecreatescaledbitmap(native method) 06-27 12:30:10.013: e/androidruntime(11265): @ android.graphics.bitmap.createscaledbitmap(bitmap.java:809) 06-27 12:30:10.013: e/androidruntime(11265): @ com.gy.bll.jsonparser.makeguirow(jsonparser.java:516) 06-27 12:30:10.013: e/androidruntime(11265): @ com.gy.bll.jsonparser.access$1(jsonparser.java:332) 06-27 12:30:10.013: e/androidruntime(11265): @ com.gy.bll.jsonparser$jsonpostasynctask.doinbackground(jsonparser.java:299) 06-27 12:30:10.013: e/androidruntime(11265): @ com.gy.bll.jsonparser$jsonpostasynctask.doinbackground(jsonparser.java:1) 06-27 12:30:10.013: e/androidruntime(11265): @ android.os.asynctask$2.call(asynctask.java:287) 06-27 12:30:10.013: e/androidruntime(11265): @ java.util.concurrent.futuretask.run(futuretask.java:234) 06-27 12:30:10.013: e/androidruntime(11265): ... 4 more
for im using android:largeheap="true" in manifest make app dont crash see in log heep grwos 250m + , make app run slow.
thanks.
- you can use image scaling mechanism.
- don't try download every time. if once downloaded store cache memory.
Comments
Post a Comment