将摄像机图像的路径保存到SQLite表中并检索它

时间:2012-03-02 12:23:03

标签: android android-camera

我昨天发布了这个问题..但不幸的是,没有人给我解决方案,我仍然不知道我的错误在哪里。

我只想将相机拍摄的图像路径保存到SQLite表中。并且我希望从数据库中检索此路径并显示图片。

代码:

public void takePic() {
    String fileName = "temp.jpg";  
    ContentValues values = new ContentValues();  
    values.put(MediaStore.Images.Media.TITLE, fileName);  
    mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

    cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    startActivityForResult(cameraIntent, Camera_requestCode);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Camera_requestCode && resultCode == RESULT_OK) {
        String[] projection = { MediaStore.Images.Media.DATA}; 
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
        int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
        cursor.moveToFirst(); 
        String capturedImageFilePath = cursor.getString(column_index_data);

        insertPath(capturedImageFilePath);
        Bitmap image = (Bitmap) data.getExtras().get("data");
    }
};

logcat的:

03-02 13:20:06.479: E/AndroidRuntime(378): FATAL EXCEPTION: main

03-02 13:20:06.479:E / AndroidRuntime(378):java.lang.RuntimeException:无法恢复活动{com.android.SaveCameraPictureDemo / com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}:java.lang.RuntimeException :无法将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给activity {com.android.SaveCameraPictureDemo / com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}:java.lang.NullPointerException 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.access $ 1600(ActivityThread.java:117) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.os.Handler.dispatchMessage(Handler.java:99) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.os.Looper.loop(Looper.java:130) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.main(ActivityThread.java:3683) 03-02 13:20:06.479:E / AndroidRuntime(378):at java.lang.reflect.Method.invokeNative(Native Method) 03-02 13:20:06.479:E / AndroidRuntime(378):at java.lang.reflect.Method.invoke(Method.java:507) 03-02 13:20:06.479:E / AndroidRuntime(378):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 03-02 13:20:06.479:E / AndroidRuntime(378):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-02 13:20:06.479:E / AndroidRuntime(378):at dalvik.system.NativeStart.main(Native Method) 03-02 13:20:06.479:E / AndroidRuntime(378):引起:java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给activity { com.android.SaveCameraPictureDemo / com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}:java.lang.NullPointerException 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107) 03-02 13:20:06.479:E / AndroidRuntime(378):......还有13个 03-02 13:20:06.479:E / AndroidRuntime(378):引起:java.lang.NullPointerException 03-02 13:20:06.479:E / AndroidRuntime(378):在android.content.ContentResolver.acquireProvider(ContentResolver.java:743) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.content.ContentResolver.query(ContentResolver.java:256) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.Activity.managedQuery(Activity.java:1550) 03-02 13:20:06.479:E / AndroidRuntime(378):at com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity.onActivityResult(SaveCameraPictureDemoActivity.java:67) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.Activity.dispatchActivityResult(Activity.java:3908) 03-02 13:20:06.479:E / AndroidRuntime(378):在android.app.ActivityThread.deliverResults(ActivityThread.java:2528) 03-02 13:20:06.479:E / AndroidRuntime(378):... 14更多 03-02 13:36:55.168:E / AndroidRuntime(434):致命异常:主要 03-02 13:36:55.168:E / AndroidRuntime(434):java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给activity {com.android .SaveCameraPictureDemo / com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}:java.lang.NullPointerException 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2574) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread.access $ 2000(ActivityThread.java:117) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:961) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.os.Handler.dispatchMessage(Handler.java:99) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.os.Looper.loop(Looper.java:130) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread.main(ActivityThread.java:3683) 03-02 13:36:55.168:E / AndroidRuntime(434):at java.lang.reflect.Method.invokeNative(Native Method) 03-02 13:36:55.168:E / AndroidRuntime(434):at java.lang.reflect.Method.invoke(Method.java:507) 03-02 13:36:55.168:E / AndroidRuntime(434):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 03-02 13:36:55.168:E / AndroidRuntime(434):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-02 13:36:55.168:E / AndroidRuntime(434):at dalvik.system.NativeStart.main(Native Method) 03-02 13:36:55.168:E / AndroidRuntime(434):引起:java.lang.NullPointerException 03-02 13:36:55.168:E / AndroidRuntime(434):at com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity.onActivityResult(SaveCameraPictureDemoActivity.java:73) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.Activity.dispatchActivityResult(Activity.java:3908) 03-02 13:36:55.168:E / AndroidRuntime(434):在android.app.ActivityThread.deliverResults(ActivityThread.java:2528) 03-02 13:36:55.168:E / AndroidRuntime(434):... 11更多

1 个答案:

答案 0 :(得分:1)

更改您的代码,

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Camera_requestCode && resultCode == RESULT_OK) {

       if(data != null)
       {
        String[] projection = { MediaStore.Images.Media.DATA}; 
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
        int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
        cursor.moveToFirst(); 
        String capturedImageFilePath = cursor.getString(column_index_data);

        insertPath(capturedImageFilePath);
        Bitmap image = (Bitmap) data.getExtras().get("data");
      }
     else
     {
       Log.e("OnActivityResult()", "data is null");
     }
    }
};