camera - Take picture with headphones button -
i want take pictures camera when headphones button pressed.
but i'm getting runtime error saying "unable start receiver...takepicturefailed".
mi mainactivity:
public class mainactivity extends appcompatactivity{ protected static camera mycamera; private camerapreview mpreview; int icantcameras; int iactivecamera = 0; camera.parameters cpparameters; audiomanager am; static context context; public static string appname; private static final string tag = "mainactivity"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); appname = getstring(r.string.app_name_long); context = getapplicationcontext(); // register , unregister receiver of headset button = (audiomanager) this.getsystemservice(context.audio_service); } @override protected void onresume(){ super.onresume(); // check if there's camera if(!checkcamerahardware(this)){ toast.maketext(this, r.string.no_camera_message, toast.length_short).show(); return; } // start listening button presses am.registermediabuttoneventreceiver(new componentname(this, receiver.class)); // number of cameras icantcameras = camera.getnumberofcameras(); // activate camera if (icantcameras > 0) { activatecamera(iactivecamera); } } public static void takep(){ // image camera mycamera.takepicture(null, null, mpicture); //toast.maketext(context,"botón presionado", toast.length_short).show(); } public void takephoto(view view){ // image camera mycamera.takepicture(null, null, mpicture); } public void showflashoptions(view view){ } // change between cameras public void switchcamera(view view){ // return if there less 2 cameras if (icantcameras < 2) { return; } if (iactivecamera == camera.camerainfo.camera_facing_back) { iactivecamera = camera.camerainfo.camera_facing_front; } else { iactivecamera = camera.camerainfo.camera_facing_back; } activatecamera(iactivecamera); } // activate indicated camera public void activatecamera(int cameraid){ // instance of camera releasecamera(); mycamera = getcamerainstance(cameraid); if(mycamera == null){ toast.maketext(this, r.string.no_access_to_camera, toast.length_short).show(); return; } // camera parameters cpparameters = mycamera.getparameters(); // set orientation of camera setcameradisplayorientation(this, 0, mycamera); // create our preview view , set content of our activity. mpreview = new camerapreview(this, mycamera); framelayout preview = (framelayout) findviewbyid(r.id.camera_preview); preview.removeallviews(); // ensures switch of cameras calls mpreview.surfacechanged method preview.addview(mpreview); } /** check if device has camera */ private boolean checkcamerahardware(context context) { if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera)){ // device has camera return true; } else { // no camera on device return false; } } /** safe way instance of camera object. */ public static camera getcamerainstance(int cameraid){ camera c = null; try { c = camera.open(cameraid); // attempt camera instance } catch (exception e){ // camera not available (in use or not exist) } return c; // returns null if camera unavailable } // sets orientation of camera display public static void setcameradisplayorientation(activity activity, int cameraid, android.hardware.camera camera) { android.hardware.camera.camerainfo info = new android.hardware.camera.camerainfo(); android.hardware.camera.getcamerainfo(cameraid, info); int rotation = activity.getwindowmanager().getdefaultdisplay() .getrotation(); int degrees = 0; switch (rotation) { case surface.rotation_0: degrees = 0; break; case surface.rotation_90: degrees = 90; break; case surface.rotation_180: degrees = 180; break; case surface.rotation_270: degrees = 270; break; } int result; if (info.facing == camera.camerainfo.camera_facing_front) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setdisplayorientation(result); } // release camera other applications private void releasecamera(){ if (mycamera != null){ mycamera.stoppreview(); mycamera.setpreviewcallback(null); mycamera.release(); mycamera = null; } } // saves picture storage public static camera.picturecallback mpicture = new camera.picturecallback() { @override public void onpicturetaken(byte[] data, camera camera) { file picturefile = getoutputmediafile(); if (picturefile == null) { log.d(tag, "error creating media file, check storage permissions"); return; } try { fileoutputstream fos = new fileoutputstream(picturefile); fos.write(data); fos.close(); } catch (filenotfoundexception e) { log.d(tag, "file not found: " + e.getmessage()); } catch (ioexception e) { log.d(tag, "error accessing file: " + e.getmessage()); } // gets picture media library mediascanner client = new mediascanner(context, picturefile, null); } }; /** create file saving image */ private static file getoutputmediafile(){ // safe, should check sdcard mounted // using environment.getexternalstoragestate() before doing this. file mediastoragedir = new file(environment.getexternalstoragepublicdirectory( environment.directory_pictures), appname); // create storage directory if not exist if (! mediastoragedir.exists()){ if (! mediastoragedir.mkdirs()){ log.d(appname, "failed create directory"); return null; } } // create media file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); file mediafile; mediafile = new file(mediastoragedir.getpath() + file.separator + "img_"+ timestamp + ".jpg"); return mediafile; } @override protected void onstop(){ super.onstop(); releasecamera(); // stop listening button presses am.unregistermediabuttoneventreceiver(new componentname(this, receiver.class)); } @override protected void ondestroy(){ super.ondestroy(); releasecamera(); // stop listening button presses am.unregistermediabuttoneventreceiver(new componentname(this, receiver.class)); }
}
my preview class:
public class camerapreview extends surfaceview implements surfaceholder.callback { private surfaceholder mholder; private camera mcamera; public camerapreview(context context, camera camera) { super(context); mcamera = camera; // install surfaceholder.callback notified when // underlying surface created , destroyed. mholder = getholder(); mholder.addcallback(this); // deprecated setting, required on android versions prior 3.0 mholder.settype(surfaceholder.surface_type_push_buffers); } public void surfacecreated(surfaceholder holder) { // surface has been created, tell camera draw preview. try { mcamera.setpreviewdisplay(holder); mcamera.startpreview(); } catch (ioexception e) { log.d("tag", "error setting camera preview: " + e.getmessage()); } } public void surfacedestroyed(surfaceholder holder) { // empty. take care of releasing camera preview in activity. } public void surfacechanged(surfaceholder holder, int format, int w, int h) { // if preview can change or rotate, take care of events here. // make sure stop preview before resizing or reformatting it. if (mholder.getsurface() == null){ // preview surface not exist return; } // stop preview before making changes try { mcamera.stoppreview(); } catch (exception e){ // ignore: tried stop non-existent preview } // set preview size , make resize, rotate or // reformatting changes here // start preview new settings try { mcamera.setpreviewdisplay(mholder); mcamera.startpreview(); } catch (exception e){ log.d("tag", "error starting camera preview: " + e.getmessage()); } }
}
my receiver class, call takep() method of mainactivity class:
public class receiver extends broadcastreceiver { // constructor mandatory public receiver () { super (); } @override public void onreceive(context context, intent intent) { if (intent.action_media_button.equals(intent.getaction())) { keyevent event = (keyevent)intent.getparcelableextra(intent.extra_key_event); int key = event.getkeycode(); switch (key) { case keyevent.keycode_break: case keyevent.keycode_media_play: case keyevent.keycode_media_play_pause: case keyevent.keycode_media_stop: case keyevent.keycode_headsethook: mainactivity.takep(); break; default: break; } } }
}
my logical:
10-30 20:10:43.257 28252-28252/? e/androidruntime: fatal exception: main 10-30 20:10:43.257 28252-28252/? e/androidruntime: process: com.garanal.photoclick, pid: 28252 10-30 20:10:43.257 28252-28252/? e/androidruntime: java.lang.runtimeexception: unable start receiver com.garanal.photoclick.receiver: java.lang.runtimeexception: takepicture failed 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.handlereceiver(activitythread.java:2680) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.access$1700(activitythread.java:156) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1428) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.os.looper.loop(looper.java:211) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.main(activitythread.java:5389) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ java.lang.reflect.method.invoke(native method) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1020) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:815) 10-30 20:10:43.257 28252-28252/? e/androidruntime: caused by: java.lang.runtimeexception: takepicture failed 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.hardware.camera.native_takepicture(native method) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.hardware.camera.takepicture(camera.java:1475) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.hardware.camera.takepicture(camera.java:1420) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.garanal.photoclick.mainactivity.takep(mainactivity.java:82) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.garanal.photoclick.receiver.onreceive(receiver.java:33) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.handlereceiver(activitythread.java:2673) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.access$1700(activitythread.java:156) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1428) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.os.looper.loop(looper.java:211) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ android.app.activitythread.main(activitythread.java:5389) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ java.lang.reflect.method.invoke(native method) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ java.lang.reflect.method.invoke(method.java:372) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1020) 10-30 20:10:43.257 28252-28252/? e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:815)
thank can give me , greetings medellin, colombia.
Comments
Post a Comment