Android:Take pictures to send them with WebService -
i want take number of photos and, when completing , send of them on webservice without keeping them on device.
i tried deleting them after sending didn't succeed.
what best way this?
this image capturing activity.
private imageview imgpreview; private button btncapturepicture; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.image_capture); context = getapplicationcontext(); imgpreview = (imageview) findviewbyid(r.id.imgpreview); btncapturepicture = (button) findviewbyid(r.id.btncapturepicture); /** * capture image button click event * */ btncapturepicture.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // capture picture captureimage(); } }); } gpstracker gps; @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // if result capturing image if (requestcode == camera_capture_image_request_code) { //todo gps = new gpstracker(imagecaptureactivity.this); if (resultcode == result_ok) { // captured image // display in image view previewcapturedimage(); //get gps location coordinates if(gps.cangetlocation()) { double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); //from latitude , longitude address geocoder gcd = new geocoder(context, locale.getdefault()); list<address> addresses = null; try { addresses = gcd.getfromlocation(latitude, longitude, 1); } catch (ioexception e) { e.printstacktrace(); } if (addresses.size() > 0) { string tara = addresses.get(0).getcountryname(); string judet = addresses.get(0).getlocality(); string oras = addresses.get(0).getsublocality(); string adresa = addresses.get(0).getaddressline(0); system.out.println(tara + ", " + judet + ", " + oras + ", " + adresa + "\n"); } } else { // can't location. // gps or network not enabled. // ask user enable gps/network in settings. toast.maketext(getapplicationcontext(), "turn on gps", toast.length_long).show(); intent backintent = new intent(imagecaptureactivity.this, cont.class); startactivity(backintent); } //todo //get date of image string pathtofile = fileuri.getpath(); file file = new file(pathtofile); if(file.exists()) { string date = new simpledateformat("dd-mm-yyyy hh-mm-ss").format( new date(file.lastmodified()) ); toast.maketext(context, date, toast.length_long) .show(); } } else if (resultcode == result_canceled) { // user cancelled image capture toast.maketext(getapplicationcontext(), "cancelled", toast.length_short) .show(); } else { // failed capture image toast.maketext(getapplicationcontext(), "error!", toast.length_short) .show(); } } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); // save file url in bundle null on scren orientation // changes outstate.putparcelable("file_uri", fileuri); } /* * here restore fileuri again */ @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); // file url fileuri = savedinstancestate.getparcelable("file_uri"); }
and helper methods:
private void captureimage() { intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafileuri(media_type_image); intent.putextra(mediastore.extra_output, fileuri); // start image capture intent startactivityforresult(intent, camera_capture_image_request_code); } /** * creating file uri store image/video */ public uri getoutputmediafileuri(int type) { return uri.fromfile(getoutputmediafile(type)); } /* * returning image / video */ private static file getoutputmediafile(int type) { // external sdcard location file mediastoragedir = new file( environment .getexternalstoragepublicdirectory(environment.directory_pictures), image_directory_name); // create storage directory if not exist if (!mediastoragedir.exists()) { if (!mediastoragedir.mkdirs()) { log.d(image_directory_name, "file" + image_directory_name + " not created"); return null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss", locale.getdefault()).format(new date()); file mediafilename; if (type == media_type_image) { mediafilename = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg"); } else { return null; } return mediafilename; } /* * display image path imageview */ private void previewcapturedimage() { try { imgpreview.setvisibility(view.visible); // bimatp factory bitmapfactory.options options = new bitmapfactory.options(); // downsizing image throws outofmemory exception larger // images options.insamplesize = 8; final bitmap bitmap = bitmapfactory.decodefile(fileuri.getpath(), options); imgpreview.setimagebitmap(bitmap); } catch (nullpointerexception e) { e.printstacktrace(); } }
i want send photos on rest webservice.
thanks edit!
first, approach saving image , delete correct because not practice keep image in memory.
second, presume have web rest service running on web server. if want upload image, have http call endpoint server serves , proper request method. can here more precise code example how exact call.
and finally, advise heavy uploading job in foreground service can sure os not kill thread in middle of upload process.
Comments
Post a Comment