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

时间:2012-02-29 09:28:00

标签: android sqlite android-camera

我正在尝试从当前活动切换到相机活动拍照,我想将捕获图像的路径保存到SQLite表中,以便我可以检索路径并显示图片任何活动。

1 - 如何保存图像的路径。?

2 - 如何从路径中显示图像?

这是我如何调用相机活动:                           “我仍然有同样的问题....任何建议”

public class SaveCameraPictureDemoActivity extends Activity {
/** Called when the activity is first created. */

ImageDB imgdb;
Intent cameraIntent;
ContentValues cv;
SQLiteDatabase db;
long newID;
final int Camera_requestCode = 1;
Button btn_Camera;

OnClickListener Listener01 = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        takePic();
    }
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn_Camera = (Button) findViewById(R.id.btn01);

    imgdb = new ImageDB(this);
    db = imgdb.getWritableDatabase();
    cv = new ContentValues();

    btn_Camera.setOnClickListener(Listener01);        
}

public void takePic() {
    cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, Camera_requestCode);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Camera_requestCode && resultCode == RESULT_OK) {
        String imgPath = data.getData().toString();
        insertPath(imgPath);
        Bitmap image = (Bitmap) data.getExtras().get("data");
    }
};

public void insertPath(String path) {
    cv.put("path", path);
    newID = db.insert("MPData", null, cv);
    if (newID == -1) {
        Toast.makeText(getBaseContext(), "Data Insertion Error", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getBaseContext(), "Data Inserted Successfully", Toast.LENGTH_SHORT).show();
    }
}

}

logcat的:

02-29 16:33:47.905: E/AndroidRuntime(10696): FATAL EXCEPTION: main
02-29 16:33:47.905: E/AndroidRuntime(10696): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.android.SaveCameraPictureDemo/com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}: java.lang.NullPointerException
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.os.Looper.loop(Looper.java:123)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread.main(ActivityThread.java:3691)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at java.lang.reflect.Method.invokeNative(Native Method)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at java.lang.reflect.Method.invoke(Method.java:507)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at dalvik.system.NativeStart.main(Native Method)
02-29 16:33:47.905: E/AndroidRuntime(10696): Caused by: java.lang.NullPointerException
02-29 16:33:47.905: E/AndroidRuntime(10696):    at com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity.onActivityResult(SaveCameraPictureDemoActivity.java:54)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
02-29 16:33:47.905: E/AndroidRuntime(10696):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
02-29 16:33:47.905: E/AndroidRuntime(10696):    ... 11 more

1 个答案:

答案 0 :(得分:0)

在您的活动onActivityResult()

// store this string in database..
String imagePath = data.getData().toString();

显示此图片只需使用意图

// Here imagePath is from database
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(imagePath), "image/*");
startActivity(intent);