【发布时间】:2020-10-09 00:44:12
【问题描述】:
我想在我的 SQL 服务器中添加一些图像。我的问题是来自插入还是获取图像代码?
现在我检索图像的图像视图是空白的。
这是我插入图片的代码:
else {
DatabaseHelper mDatabaseHelper = new DatabaseHelper(MainActivity.this);
Cursor cursor2 = mDatabaseHelper.GetPath();
while (cursor2.moveToNext()) {
Bitmap bitmap = BitmapFactory.decodeFile(cursor2.getString(0));
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /* Ignored for PNGs */ , blob);
byte[] bitmapdata = blob.toByteArray();
String id = getIntent().getStringExtra("id");
String query = "INSERT INTO StoresData VALUES('" + arabic + "','" + english + "','1','1','1','1','1','','" + lat + "','" + longi + "','" + mob + "','','','','','','','','0','','','','" + bitmapdata + "','" + user + "','" + passwords + "','0',NULL,'" + id + "')";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
Cursor cursor = mDatabaseHelper.DeleteDataOfTableImagesAr();
while (cursor.moveToNext()) {
Toast.makeText(MainActivity.this, "Deleted images", Toast.LENGTH_LONG).show();
}
}
}
取回图片:
Blob blob = parkingList.get(position).getStoreicon();
if (blob != null) {
byte[] byteArray = new byte[0];
try {
int length = (int) blob.length();
byteArray = blob.getBytes(1, length);
} catch (SQLException e) {
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
intent.putExtra("storeicon", bm);
} else {
}
是的,我正在将图像放入列表视图中。
检索和获取图像是在两个不同的应用程序中完成的,这就是我没有使用 SQLite 的原因。
【问题讨论】:
-
我认为你可以通过使用调试器来解决这个问题。在这一行设置断点 "byteArray = blob.getBytes(1, length);"并确保您在代码中达到了这一点。当您到达那里时,请确保长度变量不为 0。如果您没有到达那一点,那么 blob 很可能为空,您需要调试代码的“插入”部分。
-
@JoshMaag 长度工作正常我在行后设置了一个 system.out.println 并且它正在打印并且工作正常。
-
@JoshMaag 有什么想法吗?非常感谢您的帮助!
-
我想接下来我会尝试查看输入中的第一个和最后 5-10 个字节的 bitmapdata 和检索部分中的 byteArray,并确保它们匹配。如果他们不这样做,那么它可能是保存在数据库中的方式存在问题。如果它们确实匹配,那么它可能与位图工厂段或分配它有关。如果是这种情况,请检查“bm”是否为空。位图工厂的文档说返回是:“解码的位图,如果图像数据无法解码,则返回 null”(强调我的)。
-
@JoshMaag 我检查了它是否为空,你在哪里是对的。那么接下来我该怎么办?
标签: java android sql blob android-imageview