我制作了一个可以将图像更新到服务器的应用程序。有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);
}
答案 0 :(得分:0)
似乎Android无法找到合适的Intent
。
尝试此意图:
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// show message to user
}
答案 1 :(得分:0)
在调用第三方此类意图时,应始终检查 resolveActivity 。
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
引用this
答案 2 :(得分:0)
首先要检查您是否已在清单文件中添加了必需的权限,然后测试您的应用程序。即使该应用程序之后没有运行,也可能由于以下原因而发生: 1)您的设备上可能没有相机 2)手机中没有SD卡
的解决方案答案 3 :(得分:0)
很有可能您必须忘记添加运行时权限以访问Camera 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了解更多详情。
希望这会有所帮助。