multithreading - Android - AsyncTask fatal exception -


i load image in bitmap javacameraview. 1 send async task in order send php file. during first-second there no problem. after time, have problem of thread don't find solution. logcat :

    07-25 16:36:29.492: e/androidruntime(17507): fatal exception: asynctask #5     07-25 16:36:29.492: e/androidruntime(17507): java.lang.runtimeexception: error occured while executing doinbackground()     07-25 16:36:29.492: e/androidruntime(17507):    @   android.os.asynctask$3.done(asynctask.java:299)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.futuretask.setexception(futuretask.java:124)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.futuretask.run(futuretask.java:137)     07-25 16:36:29.492: e/androidruntime(17507):    @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.lang.thread.run(thread.java:856)     07-25 16:36:29.492: e/androidruntime(17507): caused by: java.lang.outofmemoryerror     07-25 16:36:29.492: e/androidruntime(17507):    @ java.lang.string.<init>(string.java:375)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.lang.string.<init>(string.java:238)     07-25 16:36:29.492: e/androidruntime(17507):    @ android.util.base64.encodetostring(base64.java:456)     07-25 16:36:29.492: e/androidruntime(17507):    @ com.example.objectdetect.senddata.getstringfrombitmap(senddata.java:186)     07-25 16:36:29.492: e/androidruntime(17507):    @ com.example.objectdetect.senddata.doinbackground(senddata.java:63)     07-25 16:36:29.492: e/androidruntime(17507):    @ com.example.objectdetect.senddata.doinbackground(senddata.java:1)     07-25 16:36:29.492: e/androidruntime(17507):    @ android.os.asynctask$2.call(asynctask.java:287)     07-25 16:36:29.492: e/androidruntime(17507):    @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305)     07-25 16:36:29.492: e/androidruntime(17507):    ... 5 more 

i think problem in function don't know why :

private string getstringfrombitmap(bitmap bitmappicture) {      if(!bitmappicture.isrecycled())      {          final int compression_quality = 100;          string encodedimage;          bytearrayoutputstream bytearraybitmapstream = new bytearrayoutputstream();          bitmappicture.compress(bitmap.compressformat.png, compression_quality, bytearraybitmapstream);          byte[] b = bytearraybitmapstream.tobytearray();          encodedimage = base64.encodetostring(b, base64.default);          return encodedimage;      }      else      {          return null;      } } 

after lot of test, have different logcat it's problem of memory. create bitmap in thread in run function.

herebelow logcat :

    07-26 09:42:50.491: e/androidruntime(31291): fatal exception: thread-27487     07-26 09:42:50.491: e/androidruntime(31291): java.lang.outofmemoryerror     07-26 09:42:50.491: e/androidruntime(31291):    @ android.graphics.bitmap.nativecreate(native method)     07-26 09:42:50.491: e/androidruntime(31291):    @ android.graphics.bitmap.createbitmap(bitmap.java:640)     07-26 09:42:50.491: e/androidruntime(31291):    @ android.graphics.bitmap.createbitmap(bitmap.java:620)     07-26 09:42:50.491: e/androidruntime(31291):    @ com.example.objectdetection.objectdetectionview$1.run(objectdetectionview.java:197) 

thank in advance help.

the following code came avoid out of memory errors when loading bitmaps.

   private bitmap loadimagefromnetwork(string urltouse) {     bitmap img = null;     url url;     try {        //a uniform resource locator aka place data               //located        url = new url(urltouse);        //opens httpurlconnection , downloads input stream        //bitmap        final bitmapfactory.options options = new bitmapfactory.options();        // prevents loading of image memory , ascertains bounds , memory footprint        options.injustdecodebounds = true;         bitmapfactory.decodestream(url.openstream(),null,options); // gather image data        options.insamplesize = photo.calculateinsamplesize(options,                 photo.thumbnail_width, photo.thumbnail_height);        options.injustdecodebounds = false;        img = bitmapfactory.decodestream(url.openstream(),null,options);      } catch (malformedurlexception e) {        log.i("par", "url malformed");        e.printstacktrace();     } catch (ioexception e) {        log.i("par", "failed decode bitmap");        e.printstacktrace();     }      return img;  } // end load function   public static int calculateinsamplesize(     bitmapfactory.options options, int reqwidth, int reqheight) {     // raw height , 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);         } // end if     } // end out if     return insamplesize; } 

the key thing ascertain here the initial load doesn't include entire image. flag injustdecodebounds set true, sounds. decodes bounds can determine ahead of time if image may large. suggestion determine bounds , other relevant data, tweaking before loading of image. example, converting large images thumbnails.

using technique should reduce image sizes before compress them. suppose tradeoff reducing images on load, mean lower quality. but, if running out of memory, viable alternative if willing shrink images bit.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -