首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FlashLight应用程序无法连接到照相机服务

FlashLight应用程序无法连接到照相机服务
EN

Stack Overflow用户
提问于 2017-09-30 12:30:25
回答 2查看 114关注 0票数 1

这是我的密码:

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

我做了每一件事,但每次都会崩溃

我还在我的舱单上增加了对照相机的许可。

代码语言:javascript
复制
                    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);

代码语言:javascript
复制
    //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:致命异常:主要进程: inducesmile.com.androidflashlightapp,PID: 11339 java.lang.RuntimeException:无法恢复活动{inducesmile.com.androidflashlightapp/inducesmile.com.androidflashlightapp.MainActivity}:java.lang.RuntimeException:未能连接到照相机服务在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) at android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) at android.os.Handler.dispatchMessage(Handler.java:110)在android.app.ActivityThread.main(ActivityThread.java:6251)的android.os.Looper.loop(Looper.java:203)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073)的java.lang.reflect.Method.invoke(原生方法)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934),由: java.lang.RuntimeException:未能连接到照相机服务android.hardware.Camera.(Camera.java:647) at android.hardware.Camera.open(Camera.java:510)在inducesmile.com.androidflashlightapp.MainActivity.turnOffTheFlash(MainActivity.java:105) at inducesmile.com.androidflashlightapp.MainActivity.onResume(MainActivity.java:165)在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) at android.app.Activity.performResume(Activity.java:6791)在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) at 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.app.ActivityThread.main(ActivityThread.java:6251)的android.os.Looper.loop(Looper.java:203)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1073)的java.lang.reflect.Method.invoke(本地方法)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 09-30 18:59:31.804 11339-11347/inducesmile.com.androidflashlightapp I/art: Enter循环。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-30 16:30:14

几个月前,我还开发了一个基于手电筒传感器的应用程序。我已经为手电筒活动的代码(包括java和xml)创建了gist,而且它似乎运行得很好。看看下面的链接,看看它是否有帮助:

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

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

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

代码语言:javascript
复制
askForPermissions();
if(checkForPermission()){
    //Do your processing here
}

这些职能是:

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2017-09-30 14:19:08

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46502721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档