hammas naik
hammas naik

Reputation: 25

FlashLight App Fail to connect to camera service

Here Is my Code:

I have Added a github Permission code But it still Crashes

I have done every thing but it crashes Every Time

I also have added Permission for Camera in my manifest

                    parameter = camera.getParameters();
                }
                @Override public void onPermissionDenied(PermissionDeniedResponse response) {
                    Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("App needs permission to access camera")
                            .setPositiveButton("Granted", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
                                    myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
                                    myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(myAppSettings);

                                }
                            }).setNegativeButton("Denied", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    }).create().show();
                }
                @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token)
                {[enter image description here][1]
                    token.continuePermissionRequest();
                }

            }).check();





    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    textview = (TextView) findViewById(R.id.textView);
    flashLight = (ImageButton) findViewById(R.id.flash_light);

// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    //askPermission(CAMERA,camera1);






    flashLight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isFlashLightOn) {
                turnOnTheFlash();
            } else {
                turnOffTheFlash();
            }
        }
    });

logCat:

09-30 18:59:31.698 11339-11339/inducesmile.com.androidflashlightapp E/AndroidRuntime: FATAL EXCEPTION: main Process: inducesmile.com.androidflashlightapp, PID: 11339 java.lang.RuntimeException: Unable to resume activity {inducesmile.com.androidflashlightapp/inducesmile.com.androidflashlightapp.MainActivity}: java.lang.RuntimeException: Fail to connect to camera service at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) Caused by: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:647) at android.hardware.Camera.open(Camera.java:510) at inducesmile.com.androidflashlightapp.MainActivity.turnOffTheFlash(MainActivity.java:105) at inducesmile.com.androidflashlightapp.MainActivity.onResume(MainActivity.java:165) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) at android.app.Activity.performResume(Activity.java:6791) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)  at android.os.Handler.dispatchMessage(Handler.java:110)  at android.os.Looper.loop(Looper.java:203)  at android.app.ActivityThread.main(ActivityThread.java:6251)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)  09-30 18:59:31.804 11339-11347/inducesmile.com.androidflashlightapp I/art: Enter while loop.

Upvotes: 1

Views: 128

Answers (2)

Robillo
Robillo

Reputation: 212

I also had created a flashlight sensor based app a few months back. I have created gist for the code of the flashlight activity (both java and xml) and it seems to be working fine. Take a look at the below links and see if it helps:

https://gist.github.com/robillo/b27d37be3262164ee7f5532230c28c5a

https://gist.github.com/robillo/71afef65923138ed9d6011e3bd216249

Also, try doing your processing part of the activity in the if block in onCreate() like:

askForPermissions();
if(checkForPermission()){
    //Do your processing here
}

The functions are:

void askForPermissions(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            getActivity().requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }
}

boolean checkForPermission(){
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

Upvotes: 1

Daniel
Daniel

Reputation: 56

It's tough to tell without seeing your turnOnTheFlash and turnOffTheFlash functions, but I'd guess that you are not properly releasing the camera at some point as shown by the documentation.

Upvotes: 1

Related Questions