Android-将相机图像文件路径存储到SQLite db中。

Android-将相机图像文件路径存储到SQLite db中。,sqlite,android-sqlite,android-imageview,android-image,Sqlite,Android Sqlite,Android Imageview,Android Image,我一直在试图理解SQLite DB在android中是如何工作的。我制作了一个简单的应用程序,使用相机拍摄照片并显示在屏幕上。。。我想做的是也存储照片的描述——我知道的最好的方法是使用db。但是我很难理解如何存储图像 相机的图像有点大,所以我不想把它作为一个斑点存储在数据库中。我一直在研究如何将文件路径存储为数据库中的文本字段,我认为SQL存储路径是正确的。这是我的DBAdapter类: private static final String DATABASE_CREATE_SQL =

我一直在试图理解SQLite DB在android中是如何工作的。我制作了一个简单的应用程序,使用相机拍摄照片并显示在屏幕上。。。我想做的是也存储照片的描述——我知道的最好的方法是使用db。但是我很难理解如何存储图像

相机的图像有点大,所以我不想把它作为一个斑点存储在数据库中。我一直在研究如何将文件路径存储为数据库中的文本字段,我认为SQL存储路径是正确的。这是我的DBAdapter类:

    private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_USERNAME + " text, "
        + KEY_DATE + " text, "
        + KEY_PHOTO_DESCRIPTION + " text, "
        + KEY_IMAGE_PATH + " text"
        + ");";
插入方法:

    public long insertRow(String username, String date, String photoDesc, String imagePath) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_USERNAME, username);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_PHOTO_DESCRIPTION, photoDesc);
    initialValues.put(KEY_IMAGE_PATH, imagePath);

    // Insert it into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}
然后在我的主要活动中--用于从相机接收图像的onActivityResult():

private void receiveImage(int resultCode) {
    if(resultCode == Activity.RESULT_OK) {
        getContentResolver().notifyChange(imageUri, null);
        ContentResolver contentResolver = getContentResolver();
        try {
            imageBitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri);
        } catch(Exception e) {
            Toast.makeText(TakePhotoActivity.this, "failed to load", Toast.LENGTH_LONG).show();
            Log.e(LOG_TAG, e.toString());
        }
    }
}
以下是我将行添加到数据库的方式:

public void onClick_AddRecord(View v) {
    long newId = myDb.insertRow("User", "00/00/0000",
            "image description", imageUri);
    populateListViewFromDB();
}

现在的问题是——如果我创建一个活动来查看存储在一行中的所有信息,我如何访问图像并将其分配给ImageView?

请检查它是否有帮助:

  • 编写一个方法,将images表中的所有行返回到游标中
  • 创建光标适配器并将其绑定到列表视图
  • 使用
    ImageView.setImageURI()
    将图像URI与ImageView绑定