我想将一些图像添加到我的 SQL 服务器中。我的问题是插入还是获取图像代码?
目前,我检索图像的 ImageView 是空白的。
这是我插入图像的代码:
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 {
}
是的,我正在将图像放入 ListView 中。
检索和获取图像是在两个不同的应用程序中完成的,这就是我没有使用 SQLite 的原因。
最佳答案
我无法添加评论(声誉太低),但我认为
int length = (int) blob.length();
byteArray = blob.getBytes(1, length);
lenght 正如你所说,.getBytes 从 1 开始到 lenght 并将其存储在数组中...... 但是:
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length)
不应该
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length - 1)
? 如长度从1开始,数组从0开始。
关于java - 将图像插入sql server并检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62458793/