通常在数据库中存储位图/图像不是一个好主意,它根本没有效率。
您应该将位图保存为图像并将路径存储在数据库中。
对此进行了讨论这个问题 https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay.
EDIT:
但是...如果您确实出于某种原因想要存储它,您也可以尝试将图像编码为 Base64String
,并将其存储在您的数据库中。
安卓有Base64 class http://developer.android.com/reference/android/util/Base64.html为了这。尝试使用以下代码片段进行编码:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
byte[] bai = baos.toByteArray();
String base64Image = Base64.encodeToString(bai, Base64.DEFAULT);
// Call your method to save this string on the DB here.
并且您必须对其进行解码,尝试以下操作:
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
// Now do whatever you want with the Bitmap.
您可以查看以下文档Bitmap
class here http://developer.android.com/reference/android/graphics/Bitmap.html.