android - Null pointer exception when compressing image -


when on samsung galaxy s3 select image gallery, ocasionaly recieve null pointer exception on compressing stage.

problem line

bm.compress(compressformat.jpeg, 85, bos); 

method

public string savefile(bitmap bm, int id) {         string file_path = environment.getexternalstoragedirectory()                 .getabsolutepath() + "/mk";         file dir = new file(file_path);         if (!dir.exists()) {             dir.mkdirs();         }         file file = new file(dir, "smaller" + id + ".jpeg");         fileoutputstream fout;         try {             fout = new fileoutputstream(file);             // bm.compress(bitmap.compressformat.png, 85, fout);             bufferedoutputstream bos = new bufferedoutputstream(fout);             bm.compress(compressformat.jpeg, 85, bos);             if (bos != null) {                 bos.flush();                 bos.close();             }             if (fout != null) {                 fout.flush();                 fout.close();             }             if (id == 1) {                 photo1 = file.getpath();             } else if (id == 2) {                 photo2 = file.getpath();             } else if (id == 3) {                 photo3 = file.getpath();             }         } catch (filenotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          return "";      } 

error

08-09 10:31:06.740: e/androidruntime(16358): fatal exception: main 08-09 10:31:06.740: e/androidruntime(16358): java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=11, result=-1, data=intent { dat=content://com.sec.android.gallery3d.provider/picasa/item/5813366421292566690 (has extras) }} activity {ua.mirkvartir.android.frontend/ua.mirkvartir.android.frontend.addfillactivityapp}: java.lang.nullpointerexception 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread.deliverresults(activitythread.java:3182) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread.handlesendresult(activitythread.java:3225) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread.access$1100(activitythread.java:140) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread$h.handlemessage(activitythread.java:1275) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.os.handler.dispatchmessage(handler.java:99) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.os.looper.loop(looper.java:137) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread.main(activitythread.java:4898) 08-09 10:31:06.740: e/androidruntime(16358):    @ java.lang.reflect.method.invokenative(native method) 08-09 10:31:06.740: e/androidruntime(16358):    @ java.lang.reflect.method.invoke(method.java:511) 08-09 10:31:06.740: e/androidruntime(16358):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 08-09 10:31:06.740: e/androidruntime(16358):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 08-09 10:31:06.740: e/androidruntime(16358):    @ dalvik.system.nativestart.main(native method) 08-09 10:31:06.740: e/androidruntime(16358): caused by: java.lang.nullpointerexception 08-09 10:31:06.740: e/androidruntime(16358):    @ ua.mirkvartir.android.frontend.addfillactivityapp.savefile(addfillactivityapp.java:856) 08-09 10:31:06.740: e/androidruntime(16358):    @ ua.mirkvartir.android.frontend.addfillactivityapp.onactivityresult(addfillactivityapp.java:801) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activity.dispatchactivityresult(activity.java:5390) 08-09 10:31:06.740: e/androidruntime(16358):    @ android.app.activitythread.deliverresults(activitythread.java:3178) 08-09 10:31:06.740: e/androidruntime(16358):    ... 11 more 

onactivityresult

protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);         string = "";         if (data == null) {             = "null";         } else             = data.tostring();         log.d("realpath", "resultcode " + resultcode + " data " + a);         if (requestcode == 12 && resultcode == result_ok) {           } else if (requestcode == 11 && resultcode == result_ok && null != data) {             uri selectedimage = data.getdata();             string picturepath = getrealpathfromuri(selectedimage);              pho1.setimagebitmap(decodesampledbitmapfromresource(picturepath,                     80, 60));             savefile(decodesampledbitmapfromresource(picturepath, 800, 800), 1);          } else if (requestcode == 22 && resultcode == result_ok) {          } else if (requestcode == 21 && resultcode == result_ok && null != data) {             uri selectedimage = data.getdata();             string picturepath = getrealpathfromuri(selectedimage);             pho2.setimagebitmap(decodesampledbitmapfromresource(picturepath,                     80, 60));             savefile(decodesampledbitmapfromresource(picturepath, 800, 800), 2);          } else if (requestcode == 32 && resultcode == result_ok) {          } else if (requestcode == 31 && resultcode == result_ok && null != data) {             uri selectedimage = data.getdata();              string picturepath = getrealpathfromuri(selectedimage);             pho3.setimagebitmap(decodesampledbitmapfromresource(picturepath,                     80, 60));              savefile(decodesampledbitmapfromresource(picturepath, 800, 800), 3);         }     } 

decodesamplebitmapfromresourse

public static bitmap decodesampledbitmapfromresource(string path,             int reqwidth, int reqheight) {          // first decode injustdecodebounds=true check dimensions         final bitmapfactory.options options = new bitmapfactory.options();         options.injustdecodebounds = true;         bitmapfactory.decodefile(path, options);          // calculate insamplesizeŠ°         options.insamplesize = calculateinsamplesize(options, reqwidth,                 reqheight);          // decode bitmap insamplesize set         options.injustdecodebounds = false;         return bitmapfactory.decodefile(path, options);     } 

your variable bm is null. it's reached in parameter, you'll either have debug/log there or make savefile method waterproof testing , handling bm null.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -