exception - android unable to resume activity on result using camera -
i have problem using camera. take photo , save on file. on result camera save photo on gallery , inside internal folder. i've got unable resume activity nullpointer exception.
with code open camera:
private void photobuttonclicked() { if (!runscreen.isadded()) { return; } intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(getpackagemanager()) != null) { // create file photo should go file photofile = null; try { photofile = createimagefile(); } catch (ioexception ex) { } if (photofile != null) { takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile)); startactivityforresult(takepictureintent, 2); } } } private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss") .format(new date()); string imagefilename = "jpeg_" + timestamp + "_"; file storagedir = environment .getexternalstoragepublicdirectory(environment.directory_pictures); // imagefile = file.createtempfile(imagefilename, /* prefix */ // ".jpg", /* suffix */ // storagedir /* directory */ // ); imagefile = new file(storagedir, imagefilename + ".jpg"); return imagefile; }
this code on result:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == 2 && resultcode == -1) { try { exifinterface exif = new exifinterface( imagefile.getabsolutepath()); int rotation = exif.getattributeint( exifinterface.tag_orientation, exifinterface.orientation_normal); int rotationindegrees = exiftodegrees(rotation); } catch (exception e) { e.printstacktrace(); } try { bitmap image = decodesampledbitmapfromresource( imagefile.getabsolutepath(), 1280, 800); fileoutputstream out = new fileoutputstream( gpsservice.getstring(r.string.urlsave) + gpsservice.getphotoname() + gpsservice.getstring(r.string.jpeg)); image.compress(bitmap.compressformat.jpeg, 30, out); image.recycle(); image = null; system.gc(); system.gc(); } catch (filenotfoundexception e) { e.printstacktrace(); } galleryaddpic(); } } private void galleryaddpic() { // intent mediascanintent = new intent( // intent.action_media_scanner_scan_file); file f = new file(imagefile.getabsolutepath()); // uri contenturi = uri.fromfile(f); // mediascanintent.setdata(contenturi); // this.sendbroadcast(mediascanintent); if (new sharedsharedpreferences(this).getsaveautomatically()) { contentvalues values = new contentvalues(); values.put(images.media.date_taken, system.currenttimemillis()); values.put(images.media.mime_type, "image/jpeg"); values.put(mediastore.mediacolumns.data, imagefile.getabsolutepath()); getcontentresolver().insert(images.media.external_content_uri, values); } else { f.delete(); } } private int exiftodegrees(int exiforientation) { if (exiforientation == exifinterface.orientation_rotate_90) { return 90; } else if (exiforientation == exifinterface.orientation_rotate_180) { return 180; } else if (exiforientation == exifinterface.orientation_rotate_270) { return 270; } return 0; } 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); } 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) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value power of 2 , // keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } return insamplesize; }
and error:
06-10 15:57:02.381: e/androidruntime(8443): fatal exception: main 06-10 15:57:02.381: e/androidruntime(8443): java.lang.runtimeexception: unable resume activity {cmd.run/cmd.run.run.specific.runtabdefault}: java.lang.nullpointerexception 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.performresumeactivity(activitythread.java:2919) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.handleresumeactivity(activitythread.java:2948) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2354) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3877) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.access$800(activitythread.java:159) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread$h.handlemessage(activitythread.java:1322) 06-10 15:57:02.381: e/androidruntime(8443): @ android.os.handler.dispatchmessage(handler.java:99) 06-10 15:57:02.381: e/androidruntime(8443): @ android.os.looper.loop(looper.java:176) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.main(activitythread.java:5419) 06-10 15:57:02.381: e/androidruntime(8443): @ java.lang.reflect.method.invokenative(native method) 06-10 15:57:02.381: e/androidruntime(8443): @ java.lang.reflect.method.invoke(method.java:525) 06-10 15:57:02.381: e/androidruntime(8443): @ com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1046) 06-10 15:57:02.381: e/androidruntime(8443): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:862) 06-10 15:57:02.381: e/androidruntime(8443): @ dalvik.system.nativestart.main(native method) 06-10 15:57:02.381: e/androidruntime(8443): caused by: java.lang.nullpointerexception 06-10 15:57:02.381: e/androidruntime(8443): @ cmd.run.run.specific.runtabdefault.onresume(runtabdefault.java:430) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.instrumentation.callactivityonresume(instrumentation.java:1209) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activity.performresume(activity.java:5450) 06-10 15:57:02.381: e/androidruntime(8443): @ android.app.activitythread.performresumeactivity(activitythread.java:2909) 06-10 15:57:02.381: e/androidruntime(8443): ... 13 more
why don't use this?
to call camera intent use on button click:
intent intent = new intent(mediastore.action_image_capture); file f = new file(android.os.environment.getexternalstoragedirectory(), "temp.jpg"); intent.putextra(mediastore.extra_output, uri.fromfile(f)); ((activity) context).startactivityforresult(intent, 1);
and in activity had canvas use method:
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); final imageview uploadarea = (imageview) attachmentdialog.findviewbyid(r.id.uploadarea); final edittext attachmenttitle = (edittext) attachmentdialog.findviewbyid(r.id.txtedittitleattach); bitmap bitmap; if (resultcode == result_ok) { if (requestcode == constants.request_image_capture) { file f = new file(environment.getexternalstoragedirectory().tostring()); (file temp : f.listfiles()) { if (temp.getname().equals("temp.jpg")) { f = temp; break; } } try { bitmapfactory.options bitmapoptions = new bitmapfactory.options(); bitmap = bitmapfactory.decodefile(f.getabsolutepath(), bitmapoptions); matrix matrix = new matrix(); matrix.postrotate(-90); bitmap rotatedbitmap = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, true); bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); rotatedbitmap.compress(bitmap.compressformat.png, 100, bytearrayoutputstream); byte[] attachmentbytes = bytearrayoutputstream.tobytearray(); attachmentdata = base64.encodetostring(attachmentbytes, base64.default); uploadarea.setimagebitmap(rotatedbitmap); attachmenttitle.settext("temp.jpeg"); string path = android.os.environment .getexternalstoragedirectory() + file.separator + "ctstemp" + file.separator + "default"; f.delete(); outputstream outfile = null; file file = new file(path, string.valueof(system.currenttimemillis()) + ".jpg"); try { outfile = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 85, outfile); outfile.flush(); outfile.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } } }
i hope help.
Comments
Post a Comment