我有一个名为“Run”的类,它扩展了Activity广告实现了“SurfaceHolder.callback”。
我正在尝试从cameraPreview拍摄照片并使用MediaStore保存拍摄照片的路径。稍后我会将该路径保存到SQLite数据库表中。
我设法显示了cameraPreview,但是当我按下按钮拍照时,应用程序崩溃了,logCat说有一个NullPointerException。它突出显示以下行
uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, cv);
我查看了一些教程,仍然无法找到我的错误。
这是保存相机拍摄图像路径的正确方法,以便我以后可以将路径保存到数据库中吗?
代码和logcat在
之下public class Run extends Activity implements SurfaceHolder.Callback{
Button btn01;
private android.hardware.Camera camera;
private SurfaceHolder holder;
SurfaceView sv01;
boolean previewing = false;
Uri uriTarget;
//final CameraSurfacePreviewDemo cameraSurfacePreviewDemo = new CameraSurfacePreviewDemo(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//This line is to, set the orientation of the screen as well as adjust the viewing perspective.
//without this line, the the default orientation is horizontal, and the viewing perspective is reversed.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
sv01 = (SurfaceView) findViewById(R.id.sv01);
//Intialize The Surface Holder Properly
this.holder = sv01.getHolder();
this.holder.addCallback(this);
this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn01 = (Button) findViewById(R.id.btn01);
btn01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback, myPictureCallbackRaw, myPictureCallbackJPG);
}
});
}
ShutterCallback myShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallbackRaw = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallbackJPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length);
ContentValues cv = new ContentValues();
cv.put(Images.Media.TITLE, "title");
cv.put(Images.Media.BUCKET_ID, "test");
cv.put(Images.Media.DESCRIPTION, "test image taken");
uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, cv);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(getBaseContext(), "ImageSaved" +uriTarget.toString(), Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = android.hardware.Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
logcat的:
03-19 09:25:58.145: E/Surface(8932): Surface::init token -2 identity 285
03-19 09:26:05.115: E/AndroidRuntime(8932): FATAL EXCEPTION: main
03-19 09:26:05.115: E/AndroidRuntime(8932): java.lang.NullPointerException
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.os.Parcel.readException(Parcel.java:1328)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.content.ContentResolver.insert(ContentResolver.java:604)
03-19 09:26:05.115: E/AndroidRuntime(8932): at com.androidbook.CameraPreviewDemo.Run$3.onPictureTaken(Run.java:95)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:571)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.os.Looper.loop(Looper.java:130)
03-19 09:26:05.115: E/AndroidRuntime(8932): at android.app.ActivityThread.main(ActivityThread.java:3691)
03-19 09:26:05.115: E/AndroidRuntime(8932): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 09:26:05.115: E/AndroidRuntime(8932): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 09:26:05.115: E/AndroidRuntime(8932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
03-19 09:26:05.115: E/AndroidRuntime(8932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
03-19 09:26:05.115: E/AndroidRuntime(8932): at dalvik.system.NativeStart.main(Native Method)