如何在数据库中存储图像?

时间:2013-06-05 11:15:43

标签: android

我正在开发一个需要在数据库中存储图像的应用程序。我已经通过将图像转换为字节来尝试它,但它是非常慢的进程。还有其他方法将图像存储在数据库中

3 个答案:

答案 0 :(得分:4)

您可以将图像路径存储到数据库中,并将图像存储在SD卡中,并从数据库中的imagepath中检索图像

答案 1 :(得分:1)

获取图像表单SD卡

if (requestCode == SD_REQUEST) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

testimage.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}

保存图片

DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this);

   dbHelper.open();
   dbHelper.createUserProfiles( byteArray);
   dbHelper.close();

DatabaseAdapter.java中的NOW

定义

public static final String U_PIC = "picture";

然后

private long createUserTableContentValues(long id,byte[] byteImage) {
        ContentValues values = new ContentValues();
        values.put(ID, id);
        values.put(U_PIC, byteImage);
return database.insert(IMAGE_INSERT, null, values);
}

答案 2 :(得分:0)

您可以以字符串格式将图像路径存储在数据库中。

相关问题