Android相机仅在获得许可后才能使用一次

时间:2018-01-18 16:14:05

标签: java android camera arcore

我尝试使用ArCore在Android设备中显示内置摄像头的内容。我已经处理了一个代码,用于在应用程序开始时询问权限。如果我提供了许可,我可以看到来自相机的当前图像流。

重新启动应用程序后,结果为黑色。唯一的解决方案是撤消设置内的相机权限。然后应用程序再次请求权限并在屏幕上显示图像流。

是否有人面临同样的问题?我是否必须刷新许可?如果我在没有检查权限的情况下执行RequestCameraPermission();,则会暂停活动并在一个周期内恢复活动。但我可以看到第一帧。

在控制台中出现以下错误:

D/ACameraDevice: Device error received, code 3, frame number 51, request ID 0, subseq ID 0android_camera.cc:1088 Camera capture failed! frame: 51 reason: 1

代码3为ERROR_CAMERA_DISABLED,具体取决于https://github.com/aosp-mirror/platform_development/blob/master/ndk/platforms/android-24/include/camera/NdkCameraDevice.h

仅限一段时间后

I/native: analytics_logger.cc:190 The AnalyticsClient.sendAnalyticsMessage() method returned false. Will retry...

E/native: analytics_logger.cc:198 Could not send event. Event will be dropped. 

在循环中调用。没有活动了。

我的代码:

public class GameActivity extends NativeActivity
{
  static GameActivity s_Instance;

  public static GameActivity GetInstance()
  {
      return s_Instance;
  }

  @Override
  public void onCreate(Bundle _SavedInstanceState)
  {
      super.onCreate(_SavedInstanceState);

      s_Instance = this;

      nativeInitializeInterface(getApplicationContext());
  }

  @Override
  public void onStart() {
      super.onStart();
  }

  @Override
  public void onResume()
  {
      super.onResume();

      if (!HasCameraPermission())
      {
          RequestCameraPermission();

          return;
      }

      RequestCameraPermission();
  }

  @Override
  protected void onPause()
  {
      super.onPause();
  }

  @Override
  public void onStop()
  {
      super.onStop();
  }

  @Override
  public void onDestroy()
  {
      super.onDestroy();
  }

  @Override
  public void onRequestPermissionsResult(int _RequestCode, String[] _Permissions, int[] _Results)
  {
      if (!HasCameraPermission())
      {
          Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG).show();

          if (!ShouldShowRequestPermissionRationale())
          {
              // Permission denied with checking "Do not ask again".
              LaunchPermissionSettings();
          }

          finish();
      }
  }

  public int GetDeviceOrientation()
  {
      return getResources().getConfiguration().orientation;
  }

  public int GetDeviceRotation()
  {
      return getWindowManager().getDefaultDisplay().getRotation();
  }

  public int GetDeviceDimensionWidth()
  {
      Point Size = new Point();

      getWindowManager().getDefaultDisplay().getSize(Size);

      return Size.x;
  }

  public int GetDeviceDimensionHeight()
  {
      Point Size = new Point();

      getWindowManager().getDefaultDisplay().getSize(Size);

      return Size.y;
  }

  public boolean HasCameraPermission()
  {
      return this.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
  }


  public void RequestCameraPermission()
  {
      this.requestPermissions(new String[] {Manifest.permission.CAMERA}, 0);
  }

  public boolean ShouldShowRequestPermissionRationale()
  {
      return this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);
  }

  public void LaunchPermissionSettings()
  {
      Intent intent = new Intent();
      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
      intent.setData(Uri.fromParts("package", this.getPackageName(), null));

      this.startActivity(intent);
  }

  public native void nativeInitializeInterface(Context context);

  static
  {
      System.loadLibrary("app_droid");
  }
}

1 个答案:

答案 0 :(得分:0)

你究竟在哪里有在屏幕上显示图像的逻辑?也许你只在授予回调权限的范围内拥有它。

(我把这个答案作为评论,但由于声誉还不能)