android - 如何从 MS SQL 数据库 varbinary(max) 检索图像到 Android 中的位图?
问题描述
我想从 MS SQL 数据库中检索图像。在数据库中它存储为类型 varbinary(max),但在 Android 中我需要将其转换为位图。
但是当我运行我的代码时,它总是抛出IllegalArgumentException: bad base-64
在数据库的列中它的存储是这样的:
0x5B42403965393862316230
但在应用程序中它总是这样返回[B@9ea047a8
。
这是保存图像的代码(运行良好):
public User saveProfilPicture(User user){
Connection con = DBConnection.connection();
try {
Bitmap captureImage = user.getProfilImage();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
captureImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
PreparedStatement update = con.prepareStatement("UPDATE dbo.[User]
SET profilImage = (CONVERT(VARBINARY(MAX), '"+ bytes +"')) WHERE userID = " + user.getUserID()+";");
update.executeUpdate();
user.setCreateStatus(true);
} catch (SQLException e2) {
e2.printStackTrace();
user.setCreateStatus(false);
}
return user;
}
这是检索图像的代码(有错误):
public User findProfilPicture(User user) {
Connection con = DBConnection.connection();
try{
PreparedStatement get = con.prepareStatement("SELECT CONVERT(VARBINARY(MAX),
[profilImage]) AS profilImage FROM dbo.[User] WHERE userID =" + user.getUserID() +";");
System.out.println(get.toString());
ResultSet rs = get.executeQuery();
if (rs.next()) {
byte[] bytesResult = rs.getBytes("profilImage");
byte[] bytes = Base64.decode(String.valueOf(bytesResult), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0, bytes.length);
user.setProfilImage(bitmap);
}
}catch (Exception e){
e.printStackTrace();
}
return user;
}
错误在哪里?你可以解释吗?谢谢。
解决方案
推荐阅读
- c++ - Linux Mount 使用 Remount 以允许读写
- javascript - 可以将数据从烧瓶发送到Javascript吗?
- python - 在python中,我想计算一个尚不存在的数字要减去多少,并且只有在我知道要起飞的数量后才会存在
- java - singleshoicedialog 中正负按钮的问题
- python - 使用 argparse 打开 json 文件?
- typescript - 使用打字稿转换我想查找并替换 this 关键字,我该如何找到它?
- python - int 对象在 python 中不可下标
- angular - patchValue with { emitEvent: false, onlySelf: true } 触发 Angular 中的 valueChanges
- flutter - 如何更改导航抽屉的工具栏颜色
- sql - 要搜索的数千个值