我正在尝试使用相机预览捕获图片并将其存储在SQL数据库中。 我面临的问题:
任何想法如何解决这个问题?感谢。
CameraActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_activity);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Create an instance of Camera
mCamera = getCameraInstance();
setCameraDisplayOrientation(this, 0, mCamera);
//mCamera.setFaceDetectionListener(new MyFaceDetectionListener());
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
database = new MySQLiteHelper(getApplicationContext());
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
private PictureCallback mPicture;
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try{
if (data != null){
database.addEntry(data);
}
}
catch(Exception e){
Log.d(TAG, e.getMessage());
}
}
};
mCamera.startPreview();
}
}
);
}
数据库添加图像和检索图像
public void addEntry(byte [] array) throws SQLiteException{
SQLiteDatabase database = this.getWritableDatabase();
String insertsql = "INSERT INTO "+TABLE_IMAGE+" ("+PICTURE_DATA+") VALUES(?)";
SQLiteStatement insertStmt = database.compileStatement(insertsql);
insertStmt.clearBindings();
insertStmt.bindBlob(1, array);
database.close();
}
public Bitmap getImage (){
SQLiteDatabase database = this.getReadableDatabase();
byte [] data = null;
String selectquery = "SELECT "+PICTURE_DATA+" FROM "+TABLE_IMAGE;
Cursor cursor = database.rawQuery(selectquery, null);
cursor.moveToFirst();
if (cursor != null && cursor.moveToFirst()){
data = cursor.getBlob(cursor.getColumnIndex(PICTURE_DATA));
}
System.out.println("data length: "+ data.length);
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
database.close();
return image;
}
CameraPreview.java
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
public void setCamera(Camera camera) {
if (mCamera == camera) { return; }
mCamera = camera;
if (mCamera != null) {
List<Size> localSizes = mCamera.getParameters().getSupportedPreviewSizes();
mSupportedPreviewSizes = localSizes;
requestLayout();
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
/*
Important: Call startPreview() to start updating the preview surface. Preview must
be started before you can take a picture.
*/
mCamera.startPreview();
}
}
}
logcat错误
11-08 20:18:17.313: E/AndroidRuntime(15066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facedetector/com.example.facedetector.DisplayImages}: java.lang.NullPointerException
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.os.Looper.loop(Looper.java:137)
11-08 20:18:17.313: E/AndroidRuntime(15066): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-08 20:18:17.313: E/AndroidRuntime(15066): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 20:18:17.313: E/AndroidRuntime(15066): at java.lang.reflect.Method.invoke(Method.java:525)
11-08 20:18:17.313: E/AndroidRuntime(15066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-08 20:18:17.313: E/AndroidRuntime(15066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 20:18:17.313: E/AndroidRuntime(15066): at dalvik.system.NativeStart.main(Native Method)
11-08 20:18:17.313: E/AndroidRuntime(15066): Caused by: java.lang.NullPointerException
编辑:
DisplayImages.java
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.single_image);
MySQLiteHelper db = new MySQLiteHelper(getApplicationContext());
ImageView v = (ImageView) findViewById(R.id.imageView);
v.setImageBitmap(db.getImage());
我正在尝试显示图像以测试我的图像是否存储在数据库中。
答案 0 :(得分:0)
我认为问题出在您的 DisplayImages 活动中。
v.setImageBitmap(db.getImage());
我认为getImage()返回 NULL ,导致 NullPointerException 。
始终使用try&amp;只要有可能发生异常,就会阻止阻塞。