我使用了Fabric
-service,并在运行我的应用程序的大多数设备上发现此错误。
错误是这样的:
致命异常:java.lang.NullPointerException尝试调用
虚拟方法'android.hardware.Camera $ Parameters
空对象引用上的android.hardware.Camera.getParameters()'
这是我的代码:
public class CameraActivity extends Activity implements SurfaceHolder.Callback{
android.hardware.Camera camera;
int moi = 0;
@InjectView(R.id.s)
SurfaceView surfaceView;
@InjectView(R.id.takeaphoto)
ImageView imageView;
SurfaceHolder surfaceHolder;
private Camera.Face[] mFaces;
// Draw rectangles and other fancy stuff:
private FaceOverlayView mFaceView;
private int mOrientation;
private float x1,x2;
static final int MIN_DISTANCE = 150;
private int mOrientationCompensation;
private OrientationEventListener mOrientationEventListener;
// Let's keep track of the display rotation and orientation also:
private int mDisplayRotation;
private int mDisplayOrientation;
Camera.PictureCallback callback;
int cameraId = 0;
Camera.ShutterCallback shutterCallback;
private Camera.FaceDetectionListener faceDetectionListener = new Camera.FaceDetectionListener() {
@Override
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
Log.d("onFaceDetection", "Number of Faces:" + faces.length);
// Update the view now!
mFaceView.setFaces(faces);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Fabric.with(this, new Crashlytics());
Fabric.with(this, new Answers());
setContentView(R.layout.camera_activity);
mFaceView = new FaceOverlayView(this);
addContentView(mFaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create and Start the OrientationListener:
mOrientationEventListener = new SimpleOrientationEventListener(this);
mOrientationEventListener.enable();
SharedPreferences pap = this.getSharedPreferences(
"AD1", Context.MODE_PRIVATE);
boolean frr = pap.getBoolean("fr", false);
if (!frr) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Tip");
builder.setMessage("Use the beautiful filters by swiping from right to left")
.setCancelable(true)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences e = getApplicationContext().getSharedPreferences(
"AD1", Context.MODE_PRIVATE);
SharedPreferences.Editor d = e.edit();
d.putBoolean("fr", true);
d.commit();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
}
ButterKnife.inject(this);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cameraImage();
}
});
callback = new android.hardware.Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, android.hardware.Camera camera) {
FileOutputStream outputStream=null;
File file_image = getDirc();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmddhhmms");
String date = simpleDateFormat.format(new Date());
String photo_file="PI_"+date+".jpg";
String file_name = file_image.getAbsolutePath()+"/"+photo_file;
File picfile=new File(file_name);
try {
outputStream=new FileOutputStream(picfile);
outputStream.write(bytes);
outputStream.close();
}catch (FileNotFoundException e){}
catch (IOException ex){}
finally {
}
refreshCamera();
refreshGallery(picfile);
try {
camera.stopPreview();
}catch (Exception e){}
try{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){}
}
};
}
private void refreshGallery(File file){
Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
}
public void refreshCamera(){
if (surfaceHolder.getSurface() == null){
return;
}
}
private class SimpleOrientationEventListener extends OrientationEventListener {
public SimpleOrientationEventListener(Context context) {
super(context, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onOrientationChanged(int i) {
// We keep the last known orientation. So if the user first orient
// the camera then point the camera to floor or sky, we still have
// the correct orientation.
if (orientation == ORIENTATION_UNKNOWN) return;
mOrientation = Util.roundOrientation(orientation, mOrientation);
// When the screen is unlocked, display rotation may change. Always
// calculate the up-to-date orientationCompensation.
int orientationCompensation = mOrientation
+ Util.getDisplayRotation(CameraActivity.this);
if (mOrientationCompensation != orientationCompensation) {
mOrientationCompensation = orientationCompensation;
mFaceView.setOrientation(mOrientationCompensation);
}
}
}
private File getDirc(){
File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
return new File(dics ,"Camera");
}
public void cameraImage(){
camera.takePicture(null , null ,callback);
MediaPlayer mediaPlayer = MediaPlayer.create(this , R.raw.sound);
mediaPlayer.start();
}
public void surfaceCreated(SurfaceHolder surfaceHolder ) {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
refreshCamera();
setDisplayOrientation();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera=null;
}
public void frontCamera(View view) {
camera.stopPreview();
camera.release();
camera=null;
if (cameraId == 0){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters = camera.getParameters();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else{
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters = camera.getParameters();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
}
private void setDisplayOrientation() {
// Now set the display orientation:
mDisplayRotation = Util.getDisplayRotation(CameraActivity.this);
mDisplayOrientation = Util.getDisplayOrientation(mDisplayRotation, 0);
camera.setDisplayOrientation(mDisplayOrientation);
if (mFaceView != null) {
mFaceView.setDisplayOrientation(mDisplayOrientation);
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.camera">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/pc"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CameraActivity"
android:screenOrientation="portrait"
>
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="e7ded6b46068e619fd1d96f6b9eeaac888fe83f5" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
最佳答案
检查设备Settings
中的应用程序,是否已提供所需的权限。就我而言,这就是Exception
的原因,我没有从Settings
授予权限。
希望对您有帮助。