【问题标题】:Handle Intent Camera处理意图相机
【发布时间】:2018-07-05 07:17:29
【问题描述】:

我制作了一个可以将图像更新到服务器的应用程序。有 2 个选项:从相机拍照并从库中选择。该代码适用于从库选项中选择,当我单击拍照时,应用程序会因这些报告而崩溃。 如何处理? 我的捕获图像意图代码:

capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            captureImage();
        }
    });


private void captureImage() {
    Intent intentCap = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intentCap.setType("image/*");
    startActivityForResult(intentCap, 0);
}

error

【问题讨论】:

  • 分享你的崩溃日志。如果您忘记了,请检查清单中的相机权限。
  • 本次活动你处理过onActivityResult吗?

标签: java android android-camera-intent


【解决方案1】:

Android 似乎找不到合适的Intent

试试这个意图

try{
   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
   startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// show message to user 
}

【讨论】:

    【解决方案2】:

    当像这样调用第三方的意图时,您应该始终检查 resolveActivity

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    

    参考this

    【讨论】:

      【解决方案3】:

      首先检查您是否在清单文件中添加了所需的权限,然后测试您的应用。即使应用程序在那之后没有运行,也可能由于以下原因而发生: 1) 您的设备上可能没有摄像头 2)您的手机中没有SD卡

      在这种情况下,您可以参考以下链接,这些链接描述了类似问题 link 1link 2 的解决方案

      【讨论】:

        【解决方案4】:

        很可能是您必须忘记添加运行时权限以访问相机 API,从而在您尝试打开相机时导致 应用程序崩溃。下面是截取的代码,您可以使用它来做同样的事情:

        public void showCamera(View view) {
        
                // BEGIN_INCLUDE(camera_permission)
                // Check if the Camera permission is already available.
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    // Camera permission has not been granted.
        
                    requestCameraPermission();
        
                } else {
        
                    // Camera permissions is already available, show the camera preview.
                    Log.i(TAG,
                            "CAMERA permission has already been granted. Displaying camera preview.");
                    showCameraPreview();
                }
                // END_INCLUDE(camera_permission)
        
            }
        
        private void requestCameraPermission() {
        
                // BEGIN_INCLUDE(camera_permission_request)
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)) {
                    // Provide an additional rationale to the user if the permission was not granted
                    // and the user would benefit from additional context for the use of the permission.
                    // For example if the user has previously denied the permission.
        
                } else {
        
                    // Camera permission has not been granted yet. Request it directly.
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                            REQUEST_CAMERA);
                }
                // END_INCLUDE(camera_permission_request)
        }
        

        Here is Google Runtime Permission Model Video更好的理解。

        Also check official documentation 了解更多详情。

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-11
          • 2011-02-13
          相关资源
          最近更新 更多