【问题标题】:FlashLight App Fail to connect to camera serviceFlashLight App 连接相机服务失败
【发布时间】:2018-03-12 04:18:05
【问题描述】:

这是我的代码:

我已经添加了一个 github 权限代码但它仍然崩溃

我做了所有的事情,但每次都崩溃

我还在清单中添加了相机权限

                    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();
            }
        }
    });

日志猫:

09-30 18:59:31.698 11339-11339/inducesmile.com.androidflashlightapp E/AndroidRuntime: 致命异常: main 进程:inductionsmile.com.androidflashlightapp,PID:11339 java.lang.RuntimeException:无法恢复活动 {inducesmile.com.androidflashlightapp/inducesmile.com.androidflashlightapp.MainActivity}:java.lang.RuntimeException:无法连接到相机服务 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6251) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 原因:java.lang.RuntimeException:无法连接到相机服务 在 android.hardware.Camera.(Camera.java:647) 在 android.hardware.Camera.open(Camera.java:510) 在inductionsmile.com.androidflashlightapp.MainActivity.turnOffTheFlash(MainActivity.java:105) 在inductionsmile.com.androidflashlightapp.MainActivity.onResume(MainActivity.java:165) 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) 在 android.app.Activity.performResume(Activity.java:6791) 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6251) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 09-30 18:59:31.804 11339-11347/inducesmile.com.androidflashlightapp I/art: 进入while循环。

【问题讨论】:

  • 请附加Logcat
  • 在编辑问题时可以使用格式化工具。如果没有正确的格式,就很难阅读 logcat。
  • 在许多潜在的重复中:stackoverflow.com/q/26305107/1531971所有这些相似的问题对您来说有何不同?
  • 这是其余的代码:
  • private void turnOffTheFlash() { parameter.setFlashMode(Parameters.FLASH_MODE_OFF); this.camera.setParameters(参数); this.camera.stopPreview(); isFlashLightOn = 假; private void turnOnTheFlash() { if (camera != null) { parameter = this.camera.getParameters();参数.setFlashMode(Parameters.FLASH_MODE_TORCH); this.camera.setParameters(参数); this.camera.startPreview(); isFlashLightOn = 真; \

标签: android


【解决方案1】:

几个月前,我还创建了一个基于手电筒传感器的应用程序。我已经为手电筒活动的代码(java 和 xml)创建了要点,它似乎工作正常。看看下面的链接,看看是否有帮助:

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

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

另外,尝试在 onCreate() 的 if 块中处理部分活动,例如:

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

功能有:

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;
}

【讨论】:

    【解决方案2】:

    如果没有看到您的 turnOnTheFlash 和 turnOffTheFlash 函数,很难判断,但我猜您在某些时候没有正确释放相机,如 documentation 所示。

    【讨论】:

    • 尝试了很多东西,但没有用
    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多