Not able to save Camera Images in Android -
i trying save image camera. not working ..
given permission
code pasted below. not sure why not saving image code seems fine taken developer.android site . please help!
public class cameraactivity extends activity { private camera mcamera; private camerapreview mpreview; private display display; private int previewsizewidth = 640; private int previewsizeheight= 480; public static final int media_type_image = 1; public static final int media_type_video = 2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_camera); // create instance of camera mcamera = getcamerainstance(); // create our preview view , set content of our activity. mpreview = new camerapreview(this, mcamera,getwindowmanager().getdefaultdisplay()); framelayout preview = (framelayout) findviewbyid(r.id.camera_preview); preview.addview(mpreview); button capturebutton = (button) findviewbyid(r.id.button1); capturebutton.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { // image camera log.d("take","picture"); mcamera.takepicture(null, null, mpicture); // mcamera.stoppreview(); // mcamera.startpreview(); } } ); } private picturecallback mpicture = new picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { log.d("tag","callabaclk start"); file picturefile = getoutputmediafile(media_type_image);; if (picturefile == null){ log.d("tag", "error creating media file, check storage permissions: "); return; } try { fileoutputstream fos = new fileoutputstream(picturefile); log.d("ok",picturefile.getabsolutepath()); bitmap bitmap = bitmapfactory.decodebytearray(data, 0,data.length); bitmap.compress(bitmap.compressformat.jpeg, 90, fos); //fos.write(data); fos.flush(); fos.close(); log.d("tag","done"); } catch (filenotfoundexception e) { log.d("test", "file not found: " + e.getmessage()); } catch (ioexception e) { log.d("test", "error accessing file: " + e.getmessage()); } } }; public static camera getcamerainstance(){ camera c = null; try { log.d("test",camera.getnumberofcameras()+""); c = camera.open(); // attempt camera instance } catch (exception e){ log.d("test", e.tostring()); // camera not available (in use or not exist) } return c; // returns null if camera unavailable } public static boolean checkcamerahardware(context context) { if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera)){ // device has camera return true; } else { // no camera on device return false; } } private static file getoutputmediafile(int type){ // safe, should check sdcard mounted // using environment.getexternalstoragestate() before doing this. // file mediastoragedir = new file(environment.getexternalstoragepublicdirectory( // environment.directory_pictures), "mycameraapp"); file sampledir = environment.getexternalstoragedirectory(); file mediastoragedir = new file(sampledir.getpath()+file.separator+"path"); // 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()){ mediastoragedir.mkdirs(); } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); file mediafile; if (type == media_type_image){ // mediafile = new file(mediastoragedir.getpath() + file.separator +"path_"+ timestamp + ".jpg"); mediafile = new file(mediastoragedir.getpath() ,"path_"+ timestamp + ".jpg"); } else if(type == media_type_video) { mediafile = new file(mediastoragedir.getpath() + file.separator + "vid_"+ timestamp + ".mp4"); } else { return null; } return mediafile; }
}
when getpath() 08-14 02:31:52.153: d/ok(4279): /storage/emulated/0/path/path_20130814_023152.jpg
after heck lot of research found need add these lines after fos.close :
file mediastoragedir = new file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), "path"); sendbroadcast(new intent(intent.action_media_mounted, uri.parse("file://"+ mediastoragedir)));
thanks njoy
Comments
Post a Comment