我在使用Android相机API时遇到了问题。我正在进行调试,因为
camera=Camera.open()
在 Android.Manifest 中配置了权限,并且后置摄像头可用,但是始终返回 null。
因此,我检查了 Android 找到的相机的可用性和数量:
Context context = getContext();
boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
int count = getNumberOfCameras();
camera = getCameraInstance();
Boolean b
(检查设备是否有相机)返回true
,这是预期的。
但同时,count
(获取可用相机数量)返回0
。
我的Java类:
package com.example.wouter.zytholog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import static android.hardware.Camera.*;
/**
* Created by wouter on 3/04/2018.
*/
public class CameraFragment extends Fragment implements SurfaceHolder.Callback {
static Camera camera = null;
PictureCallback mCallback;
Button mCaptureBtn;
SurfaceHolder mHolder;
SurfaceView mView;
final int CAMERA_REQUEST_CODE = 1;
public static CameraFragment newInstance(){
CameraFragment fragment = new CameraFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
mView = view.findViewById(R.id.surfaceView);
mHolder = mView.getHolder();
if(ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[] {android.Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
}
else{
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
mCaptureBtn = view.findViewById(R.id.capture);
mCaptureBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
capture();
}
});
mCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
Intent i = new Intent(getActivity(), showBeerActivity.class);
i.putExtra("capture", bytes);
startActivity(i);
return;
}
};
Context context = getContext();
boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
int count = getNumberOfCameras();
camera = getCameraInstance();
return view;
}
private void capture() {
camera.takePicture(null,null,mCallback);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
private Camera getCameraInstance() {
Camera c = null;
try{
c= open();
}
catch(Exception e){
}
return c; //returns null if camera is not available
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case CAMERA_REQUEST_CODE:{
if(grantResults.length >0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}else{
Toast.makeText(getContext(), "Please give us permission to the camera", Toast.LENGTH_LONG).show();
}
break;
}
}
}
}
Android.Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wouter.zytholog">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LogoScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".LoginOrRegisterActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".showBeerActivity"></activity>
</application>
</manifest>
getCameraInstance()
中隐藏异常:try{c= open();}catch(Exception e){ }
。这应该可以解释问题。附注:在您接受相机权限之前调用此函数... - AxelH