您好
我使用SurfaceView
创建了一个自定义相机应用程序,在运行应用程序时,它会冻结整个手机并显示黑屏。在AVD上运行时,会出现此错误:
java.lang.NullPointerException:尝试调用虚方法' android.hardware.Camera $参数android.hardware.Camera.getParameters()'在空对象引用上
引用 parameters = camera.getParameters();
的行
以下是代码:
CameraActivity.java
public class CameraActivity extends Activity implements SurfaceHolder.Callback{
android.hardware.Camera camera;
@InjectView(R.id.s)
SurfaceView surfaceView;
@InjectView(R.id.takeaphoto)
ImageView imageView;
SurfaceHolder surfaceHolder;
android.hardware.Camera.PictureCallback callback;
android.hardware.Camera.ShutterCallback shutterCallback;
@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);
setContentView(R.layout.camera_activity);
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 File getDirc(){
File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
return new File(dics ,"Camera");
}
public void cameraImage(){
camera.takePicture(null , null ,callback);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try{
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){
}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(30);
parameters.setPreviewSize(353 , 288);
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();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera=null;
}
}
camera_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rel">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/s">
</SurfaceView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/rel2"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/images"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentEnd="true"
android:src="@drawable/menu"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/takeaphoto"
android:src="@drawable/button"
android:layout_alignTop="@+id/images"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/rel1"
android:background="@color/colorPrimaryDark"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="30dp"
android:layout_marginEnd="10dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/switchcamera"
android:id="@+id/imageView" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>