java - App crashes on pressing the back button from a Camera Activity -
i'm trying make motion detector app captures images on motion detection. working fine & saving images. problem app crashes when press button camera activity return home activity. how fix ? here code:
public class motiondetectionactivity extends sensorsactivity { private static final string tag = "motiondetectionactivity"; private static final string enable_motion_detection="switch_md"; private static surfaceview preview = null; private static surfaceholder previewholder = null; private static camera camera = null; private static boolean inpreview = false; private static long mreferencetime = 0; private static imotiondetection detector = null; public static mediaplayer song; public static vibrator mvibrator; private static volatile atomicboolean processing = new atomicboolean(false); public int my_permissions_request_camera; /** * {@inheritdoc} */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); setcontentview(r.layout.main); sharedpreferences sharedpref = preferencemanager.getdefaultsharedpreferences(this); boolean enablemotionpref = sharedpref.getboolean(enable_motion_detection, true); song = mediaplayer.create(this, r.raw.sound); mvibrator = (vibrator)this.getsystemservice(vibrator_service); preview = (surfaceview) findviewbyid(r.id.preview); previewholder = preview.getholder(); previewholder.addcallback(surfacecallback); previewholder.settype(surfaceholder.surface_type_push_buffers); if (enablemotionpref) { if (preferences.use_rgb) { detector = new rgbmotiondetection(); } else if (preferences.use_luma) { detector = new lumamotiondetection(); } else { // using state based (aggregate map) detector = new aggregatelumamotiondetection(); } } } /** * {@inheritdoc} */ @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); } /** * {@inheritdoc} */ @override public void onpause() { super.onpause(); if(song!=null && song.isplaying()) { song.stop();} camera.setpreviewcallback(null); if (inpreview) camera.stoppreview(); inpreview = false; camera.release(); camera = null; } /** * {@inheritdoc} */ @override public void onresume() { super.onresume(); camera = camera.open(); } private previewcallback previewcallback = new previewcallback() { /** * {@inheritdoc} */ @override public void onpreviewframe(byte[] data, camera cam) { if (data == null) return; camera.size size = cam.getparameters().getpreviewsize(); if (size == null) return; if (!globaldata.isphoneinmotion()) { detectionthread thread = new detectionthread(data, size.width, size.height); thread.start(); } } }; private surfaceholder.callback surfacecallback = new surfaceholder.callback() { /** * {@inheritdoc} */ @override public void surfacecreated(surfaceholder holder) { try { camera.setpreviewdisplay(previewholder); camera.setpreviewcallback(previewcallback); } catch (throwable t) { log.e("callback", "exception in setpreviewdisplay()", t); } } /** * {@inheritdoc} */ @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { camera.parameters parameters = camera.getparameters(); camera.size size = getbestpreviewsize(width, height, parameters); if (size != null) { parameters.setpreviewsize(size.width, size.height); log.d(tag, "using width=" + size.width + " height=" + size.height); } camera.setparameters(parameters); camera.startpreview(); inpreview = true; } /** * {@inheritdoc} */ @override public void surfacedestroyed(surfaceholder holder) { // ignore } }; private static camera.size getbestpreviewsize(int width, int height, camera.parameters parameters) { camera.size result = null; (camera.size size : parameters.getsupportedpreviewsizes()) { if (size.width <= width && size.height <= height) { if (result == null) { result = size; } else { int resultarea = result.width * result.height; int newarea = size.width * size.height; if (newarea > resultarea) result = size; } } } return result; } private static final class detectionthread extends thread { private byte[] data; private int width; private int height; public detectionthread(byte[] data, int width, int height) { this.data = data; this.width = width; this.height = height; } /** * {@inheritdoc} */ @override public void run() { if (!processing.compareandset(false, true)) return; // log.d(tag, "begin processing..."); try { // previous frame int[] pre = null; if (preferences.save_previous) pre = detector.getprevious(); // current frame (with changes) // long bconversion = system.currenttimemillis(); int[] img = null; if (preferences.use_rgb) { img = imageprocessing.decodeyuv420sptorgb(data, width, height); if (img != null && detector.detect(img, width, height)) { if(song!=null && !song.isplaying()) { song.start(); mvibrator.vibrate(50); } } else { if(song!=null && song.isplaying()) { song.pause(); } } } // current frame (without changes) int[] org = null; if (preferences.save_original && img != null) org = img.clone(); if (img != null && detector.detect(img, width, height)) { // delay necessary avoid taking picture while in // // middle of taking another. problem can causes // phones // reboot. long = system.currenttimemillis(); if (now > (mreferencetime + preferences.picture_delay)) { mreferencetime = now; bitmap previous = null; if (preferences.save_previous && pre != null) { if (preferences.use_rgb) previous = imageprocessing.rgbtobitmap(pre, width, height); else previous = imageprocessing.lumatogreyscale(pre, width, height); } bitmap original = null; if (preferences.save_original && org != null) { if (preferences.use_rgb) original = imageprocessing.rgbtobitmap(org, width, height); else original = imageprocessing.lumatogreyscale(org, width, height); } bitmap bitmap = null; if (preferences.save_changes) { if (preferences.use_rgb) bitmap = imageprocessing.rgbtobitmap(img, width, height); else bitmap = imageprocessing.lumatogreyscale(img, width, height); } log.i(tag, "saving.. previous=" + previous + " original=" + original + " bitmap=" + bitmap); looper.prepare(); new savephototask().execute(previous, original, bitmap); } else { log.i(tag, "not taking picture because not enough time has passed since creation of surface"); } } } catch (exception e) { e.printstacktrace(); } { processing.set(false); } // log.d(tag, "end processing..."); processing.set(false); } }; private static final class savephototask extends asynctask<bitmap, integer, integer> { /** * {@inheritdoc} */ @override protected integer doinbackground(bitmap... data) { (int = 0; < data.length; i++) { bitmap bitmap = data[i]; string name = "motdet_"+string.valueof(system.currenttimemillis()); if (bitmap != null) createdirectoryandsavefile(name, bitmap); } return 1; } private void createdirectoryandsavefile(string name, bitmap bitmap) { file folder = new file(environment.getexternalstoragedirectory() + file.separator + "md");//here have created different name boolean success = true; if (!folder.exists()) { success = folder.mkdirs(); } if (success) { // on success } else { // else on failure } file photo = new file(folder.getabsolutepath(), name+ ".jpg"); //use path of above created folder if (photo.exists()) { photo.delete(); } try { fileoutputstream out = new fileoutputstream(photo.getpath()); bitmap.compress(bitmap.compressformat.jpeg, 100, out); out.flush(); out.close(); } catch (exception e) { e.printstacktrace(); } } } }
homeactivity
public class homeactivity extends appcompatactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); setcontentview(r.layout.home_layout); button bt1 = (button) findviewbyid(r.id.button); button bt2= (button)findviewbyid(r.id.button1); bt1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent = new intent(view.getcontext(), motiondetectionactivity.class); startactivity(i); } }); bt2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view vew) { intent b=new intent(vew.getcontext(),settingsactivity.class); startactivity(b); } }); } }
handle see if still crashes
@override public void surfacechanged(surfaceholder holder, int format, int width, int height) { if(camera != null) { camera.parameters parameters = camera.getparameters(); camera.size size = getbestpreviewsize(width, height, parameters); if (size != null) { parameters.setpreviewsize(size.width, size.height); log.d(tag, "using width=" + size.width + " height=" + size.height); } camera.setparameters(parameters); camera.startpreview(); inpreview = true; } }
Comments
Post a Comment