java - Save image taken to a specific directory with specific name -


i having trouble saving image taken specific directory specific name. have no idea how it. below code.

cameracaptureimage.java

public class capturecameraimage extends activity {      public static int cameraid = 0;     public static boolean isblack = true;     public static imageview image;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activitycapturecameraimage);         image = (imageview) findviewbyid(r.id.imgview);     }        public void onbackclick(view v){           cameraid = 0;         intent = new intent(capturecameraimage.this,cameraview.class);         startactivityforresult(i, 999);     }  } 

cameraview.java

public class cameraview extends activity implements surfaceholder.callback, onclicklistener{         private static final string tag = "cameratest";         camera mcamera;         boolean mpreviewrunning = false;          @suppresswarnings("deprecation")         public void oncreate(bundle icicle){             super.oncreate(icicle);             log.e(tag, "oncreate");              getwindow().setformat(pixelformat.translucent);             requestwindowfeature(window.feature_no_title);             getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);             setcontentview(r.layout.cameraview);             imageview img = (imageview) findviewbyid(r.id.blankimage);              if(capturecameraimage.isblack)                 img.setbackgroundresource(android.r.color.black);             else                 img.setbackgroundresource(android.r.color.white);              msurfaceview = (surfaceview) findviewbyid(r.id.surface_camera);             msurfaceview.setonclicklistener(this);             msurfaceholder = msurfaceview.getholder();             msurfaceholder.addcallback(this);             msurfaceholder.settype(surfaceholder.surface_type_push_buffers);          }          @override         protected void onrestoreinstancestate(bundle savedinstancestate){             super.onrestoreinstancestate(savedinstancestate);         }           camera.picturecallback mpicturecallback = new camera.picturecallback() {              public void onpicturetaken(byte[] data, camera camera) {                 // todo auto-generated method stub                 if (data != null){                     //intent mintent = new intent();                     //mintent.putextra("image",imagedata);                      mcamera.stoppreview();                     mpreviewrunning = false;                     mcamera.release();                       try{                          bitmapfactory.options opts = new bitmapfactory.options();                          bitmap bitmap= bitmapfactory.decodebytearray(data, 0, data.length,opts);                          bitmap = bitmap.createscaledbitmap(bitmap, 300, 300, false);                          int width = bitmap.getwidth();                          int height = bitmap.getheight();                          int newwidth = 300;                          int newheight = 300;                           // calculate scale - in case = 0.4f                          float scalewidth = ((float) newwidth) / width;                          float scaleheight = ((float) newheight) / height;                           // createa matrix manipulation                          matrix matrix = new matrix();                          // resize bit map                          matrix.postscale(scalewidth, scaleheight);                          // rotate bitmap                          matrix.postrotate(90);                          bitmap resizedbitmap = bitmap.createbitmap(bitmap, 0, 0,                                  width, height, matrix, true);                          capturecameraimage.image.setimagebitmap(resizedbitmap);                       }catch(exception e){                          e.printstacktrace();                      }                     //storebyteimage(mcontext, imagedata, 50,"imagename");                     //setresult(foto_mode, mintent);                     setresult(585);                     finish();                 }                    }         };          protected void onresume(){             log.e(tag, "onresume");             super.onresume();         }          protected void onsaveinstancestate(bundle outstate){             super.onsaveinstancestate(outstate);         }          protected void onstop(){             log.e(tag, "onstop");             super.onstop();         }          @targetapi(9)         public void surfacecreated(surfaceholder holder){             log.e(tag, "surfacecreated");             mcamera = camera.open(capturecameraimage.cameraid);         }          public void surfacechanged(surfaceholder holder, int format, int w, int h) {             log.e(tag, "surfacechanged");              // xxx stoppreview() crash if preview not running             if (mpreviewrunning){                 mcamera.stoppreview();             }              camera.parameters p = mcamera.getparameters();             p.setpreviewsize(300, 300);              if(capturecameraimage.cameraid == 0){                 string stringflashmode = p.getflashmode();                 if (stringflashmode.equals("torch"))                         p.setflashmode("on"); // light set off, flash set normal 'on' mode                 else                         p.setflashmode("torch");             }              mcamera.setparameters(p);             try{                 mcamera.setpreviewdisplay(holder);             }catch (exception e){                 // todo auto-generated catch block                 e.printstacktrace();             }             mcamera.startpreview();             mpreviewrunning = true;             mcamera.takepicture(null, mpicturecallback, mpicturecallback);         }          public void surfacedestroyed(surfaceholder holder) {             log.e(tag, "surfacedestroyed");             //mcamera.stoppreview();             //mpreviewrunning = false;             //mcamera.release();         }          private surfaceview msurfaceview;         private surfaceholder msurfaceholder;          public void onclick(view v) {             // todo auto-generated method stub             mcamera.takepicture(null, mpicturecallback, mpicturecallback);         }      } 

to capture image , save in specific folder, have :

  1. request permission on runtime (api 23)
  2. request image capture
  3. create folder

first of all, if target api 23, have follow new permission system asks user grant when used first time. in case, need have write_external_storage granted.

    boolean haspermission = (contextcompat.checkselfpermission(this,             manifest.permission.write_external_storage) == packagemanager.permission_granted);      if (!haspermission) {         activitycompat.requestpermissions(this,                 new string[]{manifest.permission.write_external_storage},                 request_write_storage);     } 

then, need create implicit intent capture photo. camera app appear in app chooser or selected automatically depending on user settings , installed apps.

intent intent = new intent(mediastore.action_image_capture);  startactivityforresult(intent, request_image_capture); // final int = 1 

finally, need create folder , indicate output android know save picture.

//intent = new intent...                      long timestamp = system.currenttimemillis();                      file file = new file("");                          file = new file(environment.getexternalstoragedirectory()                             + file.separator                             + "dcim"                             + file.separator                             + address                             + file.separator                             , "img_" + string.valueof(timestamp) + ".jpg");  intent.putextra(mediastore.extra_output, uri.fromfile(file));  //startactivityforresult... 

there lot of storage possibilities. can take here : https://developer.android.com/training/basics/data-storage/files.html

i have done nothing special in onactivityresult callback method. show toast confirm user picture saved successfully. code above in onclicklistener of imageview in case.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -