My simple camera app (android) won't load the camera view after the first attempt -
i'm trying make simple camera app uses runtime permissions...it seems load fine first time app launched (asking camera permission). after allowing access, works...but close , start again, shows white image icons won't respond. i've checked permissions manually in app , camera still granted access think i'm screwing permissions code.
here mainactivity code:
public class mainactivity extends appcompatactivity { private static final int my_permissions_request_camera = 1; private camera mcamera = null; private camera mcamerafront = null; private cameraview mcameraview = null; public int switchcamera = 1; // int permissioncheck = contextcompat.checkselfpermission(this, // manifest.permission.camera); // string[] perms = {"android.permission.camera"}; // int permsrequestcode = 200; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); if (contextcompat.checkselfpermission(this, manifest.permission.camera) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.camera)) { // show explanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. } else { // no explanation needed, can request permission. activitycompat.requestpermissions(this, new string[]{manifest.permission.camera}, my_permissions_request_camera); try { mcamera = camera.open(1);//you can use open(int) use different cameras } catch (exception e) { log.d("error", "failed camera: " + e.getmessage()); } swapcamera(); // if (mcamera != null) { //// mcameraview = new cameraview(this, mcamera);//create surfaceview show camera data //// framelayout camera_view = (framelayout)findviewbyid(r.id.camera_view); //// camera_view.addview(mcameraview);//add surfaceview layout // swapcamera(); // } //btn close application imagebutton imgclose = (imagebutton) findviewbyid(r.id.imgclose); imgclose.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mcamera.setpreviewcallback(null); mcamera.seterrorcallback(null); mcamera.stoppreview(); mcamera.release(); mcamera = null; system.exit(0); } }); // btn switch camera imagebutton imgswitch = (imagebutton) findviewbyid(r.id.cameraswitch); imgswitch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // switchcamera++; } }); } } } public void swapcamera() { mcameraview = new cameraview(this, mcamera);//create surfaceview show camera data framelayout camera_view = (framelayout)findviewbyid(r.id.camera_view); camera_view.addview(mcameraview);//add surfaceview layout } @override public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) { switch (requestcode) { case my_permissions_request_camera: { // if request cancelled, result arrays empty. if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { // permission granted, yay! // camera-related task need do. try{ mcamera = camera.open(1);//you can use open(int) use different cameras } catch (exception e){ log.d("error", "failed camera: " + e.getmessage()); } if(mcamera != null) { // mcameraview = new cameraview(this, mcamera);//create surfaceview show camera data // framelayout camera_view = (framelayout)findviewbyid(r.id.camera_view); // camera_view.addview(mcameraview);//add surfaceview layout swapcamera(); } //btn close application imagebutton imgclose = (imagebutton)findviewbyid(r.id.imgclose); imgclose.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mcamera.setpreviewcallback(null); mcamera.seterrorcallback(null); mcamera.stoppreview(); mcamera.release(); mcamera = null; system.exit(0); } }); // btn switch camera imagebutton imgswitch = (imagebutton)findviewbyid(r.id.cameraswitch); imgswitch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // switchcamera++; } }); } else { // permission denied, boo! disable // functionality depends on permission. } return; } // other 'case' lines check other // permissions app might request } } public void onactivityresult() { } }
i believe missing "else"
if (contextcompat.checkselfpermission(this, manifest.permission.camera) != packagemanager.permission_granted) { // code } else { // stuff camera try{ mcamera = camera.open(1); } catch (exception e){ ... }
Comments
Post a Comment