image - Saving Picture taken from camera while making a directory (Android App) -
so im trying 4 things: 1) take photo 2) load in imageview 3) make new directory 4) save image in directory.
the problem here:
file path = environment.getexternalstoragepublicdirectory (environment.directory_pictures); file mediastoragedir = new file(path, "mycameraapp");
for reason not making new file called mycameraapp.
and
i keep getting threadid=1: thread exiting uncaught exception (group=0x41631ba8).
here code:
public class main extends activity { private uri fileuri; imageview iv; private camera mcamera; public static final int media_type_image = 1; private static final int capture_image_activity_request_code = 100; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); iv = (imageview) findviewbyid(r.id.imageview); fileuri = getoutputmediafileuri(media_type_image); // create file save image button btn = (button) findviewbyid(r.id.takephoto); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { intent intent = new intent(android.provider.mediastore.action_image_capture); intent.putextra(mediastore.extra_output, fileuri); // set image file name startactivityforresult(intent, capture_image_activity_request_code); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data){ if (requestcode == capture_image_activity_request_code) { // image captured , saved fileuri specified in intent toast.maketext(this, "image saved to:\n" + data.getdata(), toast.length_long).show(); bitmap theimage = (bitmap) data.getextras().get("data"); iv.setimagebitmap(theimage); } } /** create file uri saving image or video */ private static uri getoutputmediafileuri(int type){ return uri.fromfile(getoutputmediafile(type)); } /** create file saving image or video */ private static file getoutputmediafile(int type){ // safe, should check sdcard mounted // using environment.getexternalstoragestate() before doing this. file path = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file mediastoragedir = new file(path, "mycameraapp"); // location works best if want created images shared // between applications , persist after app has been uninstalled. // create storage directory if not exist if (!mediastoragedir.exists()){ if (!mediastoragedir.mkdirs()){ log.d("mycameraapp", "failed create directory"); return null; } } // create media file name file mediafile = null; if (type == media_type_image){ mediafile = new file(mediastoragedir.getpath() + /*file.separator +*/ "img_1" +".tif"); log.d("mycameraapp", "done"); } else { log.d("mycameraapp", "not done!"); return null; } return mediafile; } protected void onpause(){ super.onpause(); releasecamera(); // release camera on pause event } private void releasecamera(){ if (mcamera != null){ mcamera.release(); // release camera other applications mcamera = null; } } }
the stacktrace:
03-10 17:14:39.708: i/process(9786): sending signal. pid: 9786 sig: 9 03-10 18:04:04.248: d/mycameraapp(13459): failed create directory 03-10 18:04:04.248: d/androidruntime(13459): shutting down vm 03-10 18:04:04.248: w/dalvikvm(13459): threadid=1: thread exiting uncaught exception(group=0x41631ba8) 03-10 18:04:04.258: e/androidruntime(13459): fatal exception: main 03-10 18:04:04.258: e/androidruntime(13459): process: com.example.irisrec, pid: 13459 03-10 18:04:04.258: e/androidruntime(13459): java.lang.runtimeexception: unable start activity componentinfo{com.example.irisrec/com.example.irisrec.main}: java.lang.nullpointerexception: file 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread.access$800(activitythread.java:135) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-10 18:04:04.258: e/androidruntime(13459): @ android.os.handler.dispatchmessage(handler.java:102) 03-10 18:04:04.258: e/androidruntime(13459): @ android.os.looper.loop(looper.java:136) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread.main(activitythread.java:5017) 03-10 18:04:04.258: e/androidruntime(13459): @ java.lang.reflect.method.invokenative(native method) 03-10 18:04:04.258: e/androidruntime(13459): @ java.lang.reflect.method.invoke(method.java:515) 03-10 18:04:04.258: e/androidruntime(13459): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-10 18:04:04.258: e/androidruntime(13459): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-10 18:04:04.258: e/androidruntime(13459): @ dalvik.system.nativestart.main(native method) 03-10 18:04:04.258: e/androidruntime(13459): caused by: java.lang.nullpointerexception: file 03-10 18:04:04.258: e/androidruntime(13459): @ android.net.uri.fromfile(uri.java:444) 03-10 18:04:04.258: e/androidruntime(13459): @ com.example.irisrec.main.getoutputmediafileuri(main.java:80) 03-10 18:04:04.258: e/androidruntime(13459): @ com.example.irisrec.main.oncreate(main.java:38) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activity.performcreate(activity.java:5231) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-10 18:04:04.258: e/androidruntime(13459): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159)
thanks help!!
Comments
Post a Comment