简体   繁体   English

App CameraSurface的首次启动会崩溃。 提交许可后,它可以工作

[英]First start of App CameraSurface crashes. After submitting Permission it works

I have a App which creates a TextureView with the Camera attached to it. 我有一个应用程序,该应用程序创建了一个附着有Camera的TextureView。 At the Moment, the Activity is also the MainLauncher of the Application. 此刻,活动也是应用程序的主启动器。 I wanted to use the new Permission Style just as intended and called the Permission when the Camera should be in use. 我想按预期使用新的权限样式,并在应该使用Camera时将其称为“权限”。 But when I start the App for the first Time, it calls my Permission, I accept it, and after that the App crashes ( Only Information in Logcat is SIG:9 ). 但是,当我第一次启动该应用程序时,它会调用我的权限,我接受它,然后该应用程序崩溃( Logcat中的唯一信息是SIG:9 )。 When I start it again (because I already submitted) it works fine. 当我再次启动它(因为我已经提交)时,它可以正常工作。 Did I set the call for the Permission at the wrong place? 我是否将呼叫权限设置在错误的位置? ( btw. I call multiple Permissions, because I also start to create a Image Gallery ). 顺便说一句,我叫多个权限,因为我也开始创建一个图片库 )。 I could't find similar Questions so I wanted to ask the Question now. 我找不到类似的问题,所以我现在想问这个问题。

Here is my Code: 这是我的代码:

OnCreate: OnCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    callPermissions(); // calls multiple Permissions.

    setContentView(R.layout.camera_landscape);

    mCaptureButton = (Button) this.findViewById(R.id.photoButton);
    mCalcButton = (Button) this.findViewById(R.id.descriptor);
    mTrackButton = (Button) this.findViewById(R.id.track);

    mTextureView = (AutoTextureView) findViewById(R.id.texture);
}

callPermissions: callPermissions:

private void callPermissions(){
    List<String> permissionsNeeded = new ArrayList<String>();

    final List<String> permissionsList = new ArrayList<String>();
    if(!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
        permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
    if(!addPermission(permissionsList, Manifest.permission.CAMERA))
        permissionsNeeded.add("CAMERA");

    if(permissionsList.size()> 0){
        if(permissionsNeeded.size() > 0){
            String message = "You need to grant access to " + permissionsNeeded.get(0);
            for(int i = 1; i < permissionsNeeded.size(); i++)
                message = message + ", " + permissionsNeeded.get(i);

            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(CameraTest.this, permissionsList.toArray(new String[permissionsList.size()]),
                                    REQUEST_CODE_ALL_PERMISSIONS);
                        }
                    });
            return;
        }
        ActivityCompat.requestPermissions(CameraTest.this, permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ALL_PERMISSIONS);
    }
}

And the Overriden onRequestPermissionResult: 和重写onRequestPermissionResult:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    switch(requestCode){
        case REQUEST_CAMERA_PERMISSION:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                openCamera();
            }else{
                Toast.makeText(CameraTest.this, "CAMERA access Denied", Toast.LENGTH_SHORT).show();
            }
            break;
        case REQUEST_CODE_ALL_PERMISSIONS:{
            Map<String, Integer> perms = new HashMap<String, Integer>();

            perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);

            for(int i = 0; i < permissions.length; i++)
                perms.put(permissions[i], grantResults[i]);

            if(perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                    perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){

            }else{
                //Permissions Denied
                Toast.makeText(CameraTest.this, "Some Permission is Denied", Toast.LENGTH_SHORT).show();
            }
        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

And Here the LogCat output: 这是LogCat的输出:

10-25 10:56:56.906 26719-26719/? I/art: Late-enabling -Xcheck:jni
10-25 10:56:56.926 26719-26725/? I/art: Debugger is no longer active
10-25 10:56:57.084 26719-26743/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-25 10:56:57.163 26719-26743/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
10-25 10:56:57.172 26719-26743/? I/OpenGLRenderer: Initialized EGL, version 1.4
10-25 10:56:57.331 26719-26719/? I/CameraManagerGlobal: Connecting to camera service
10-25 10:57:02.407 26719-26719/com.example.camera D/AndroidRuntime: Shutting down VM
10-25 10:57:02.408 26719-26866/com.example.camera I/Process: Sending signal. PID: 26719 SIG: 9

Well I could solve the Solution by myself, I Also have a Method (which I totally missed to add) Which is called openCamera Which configures the Camera for the TextureSurface: 好吧,我可以自己解决该解决方案,我还有一个方法(我完全openCamera添加),称为openCamera它为TextureSurface配置了Camera:

private void openCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            if(!ActivityCompat.shouldShowRequestPermissionRationale(CameraTest.this, Manifest.permission.CAMERA)){
                //showMessageOKCancel("You need to allow access to Camera", new DialogInterface.OnClickListener(){
                //    @Override
                //    public void onClick(DialogInterface dialog, int which){
                //        ActivityCompat.requestPermissions(CameraTest.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                //    }
                //});
                return;
            }
            ActivityCompat.requestPermissions(CameraTest.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
            return;
        }
        cameraManager.openCamera(mCameraId, mDeviceCallback, mBackgroundHandler);
    }catch(CameraAccessException e) {
        e.printStackTrace();
    }
}

There, I also started a Dialog, when the User has not granted the Permission yet. 当用户尚未授予权限时,我还在那里启动了一个对话框。 After I commented the showMessageOkCancel Method (which creates the dialog) everything works fine and only my callPermissions method requests my two Permissions and the Application does not crash at the first start. 在对showMessageOkCancel方法(将创建对话框)进行注释之后,一切正常,只有我的callPermissions方法请求我的两个权限,并且应用程序在第一次启动时不会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android ListView示例崩溃。 无法启动应用 - Android ListView example crashes. Can't start app 第一次安装应用程序崩溃,但重新安装后它可以工作 - At first install the app crashes, but after reinstall it works 应用程序在框架中第一次请求位置许可后立即崩溃 - App crashes right after asking for location permission for the first time in frament PhoneGap应用程序仅在首次启动后才能工作 - PhoneGap app works only after first start 试图在Android上运行Libgdx应用程序,但崩溃。 桌面版效果很好 - Trying to run Libgdx app on Android, but it crashes. Desktop version works fine 缺少应用程序崩溃的crashlytics报告。 什么时候应该调用Crashlytics.start()? - Missing crashlytics reports for app crashes. When should Crashlytics.start() be called? 应用仅适用于第一个输入,如果插入第二个输入,则崩溃 - App only works with first input, crashes after if second input is inserted Firebase Cloud Messaging 仅在应用首次启动后才有效 - Firebase Cloud Messaging works only after first start of the app 首次单击扫描按钮后,应用程序崩溃,但重新打开后该应用程序可以工作 - App crashes after clicking scan button for the first time but the app works after i reopen it 列表视图崩溃。 不工作的应用程序。 Android Studio 模拟器 - ListView crashes. Not working app. Android Studio Emulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM