如何在SQLite Android中使用Insert Into查询在Android中放置图像?
我是安卓系统的新手 我只是想知道如何将图像存储到Android中的SQLite数据库中,我的数据库是这样的如何在SQLite Android中使用Insert Into查询在Android中放置图像?,android,android-sqlite,Android,Android Sqlite,我是安卓系统的新手 我只是想知道如何将图像存储到Android中的SQLite数据库中,我的数据库是这样的 Photo id (int) | image (BLOB) 然后我有一门课从画廊里得到一张图片 LogoSQLiDemoActivity 这是我的形象课 有人能帮我解决这个问题吗,因为我一直在谷歌搜索,但我仍然无法解决我的问题,请帮助…将图像保存在本地文件夹中,并使用与您的id相同的名称。因此,现在无论何时您要检索图像,只要打开id.jpg/id.png试试这个或这个@nfirex:我认
Photo
id (int) | image (BLOB)
然后我有一门课从画廊里得到一张图片
LogoSQLiDemoActivity
这是我的形象课
有人能帮我解决这个问题吗,因为我一直在谷歌搜索,但我仍然无法解决我的问题,请帮助…将图像保存在本地文件夹中,并使用与您的id相同的名称。因此,现在无论何时您要检索图像,只要打开id.jpg/id.png试试这个或这个@nfirex:我认为这与我在课堂上的做法相同,它没有使用插入对吗?是的,你的方法从第一个链接开始,你的方法有什么问题?如果您收到异常,您可以发布它吗?@nfirex:我想在不使用该方法的情况下存储图像,所以我想知道是否可以将用户插入到查询中以将数据放入sqlite中?你能帮我吗?是的,你能。如果你想要硬核——请看第二个链接中答案的第一部分:将字节转换为hexliteral的示例:尝试这种方法,但我没有使用它
private void saveToDB() {
SQLiteDatabase myDb;
String MySQL;
byte[] byteImage1 = null;
byte[] byteImage2 = null;
MySQL = "create table emp1(_id INTEGER primary key autoincrement, sample TEXT not null, audio BLOB);";
myDb = openOrCreateDatabase("Blob List", Context.MODE_PRIVATE, null);
myDb.execSQL(MySQL);
String s = myDb.getPath();
textView.append("\r\n" + s + "\r\n");
myDb.execSQL("delete from emp1");
ContentValues newValues = new ContentValues();
newValues.put("sample", "HI Hello");
try {
InputStream is = new FileInputStream("YOUR IMAGE PATH");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
textView.append("\r\n" + bytes.length + "\r\n");
newValues.put("audio", bytes);
long ret = myDb.insert("emp1", null, newValues);
if (ret < 0)
textView.append("\r\n!!! Error add blob failed!!!\r\n");
} catch (IOException e) {
textView.append("\r\n!!! Error: " + e + "!!!\r\n");
}
Cursor cur = myDb.query("emp1", null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
textView.append("\r\n" + cur.getString(1) + "\r\n");
cur.moveToNext();
}
// /////Read data from blob field////////////////////
cur.moveToFirst();
byteImage2 = cur.getBlob(cur.getColumnIndex("audio"));
// bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0,
// byteImage2.length));
textView.append("\r\n" + byteImage2.length + "\r\n");
cur.close();
myDb.close();
}
public void addImage3(image img) {
open(); //it's mean to open the connection from database
//THE Question is right here, how i can put a byte array into database
//without using this method, a.k.a INSERT INTO, cause i have tried to search any
//solution in google, but i can't solve my problem
ContentValues values = new ContentValues();
values.put(image, img._lokasi_foto);
// Inserting Row
database.insert(Photo, null, values);
close(); // Closing database connection
}
public class image {
public byte[] _lokasi_foto;
//this is getter
public byte[] getLokasi_foto() {
return _lokasi_foto;
}
//this is setter
public void setLokasi_foto(byte[] lokasi_foto) {
this._lokasi_foto = lokasi_foto;
}
and this the constructor
public image(byte[] lokasi_foto) {
this._lokasi_foto = lokasi_foto;
}
}
private void saveToDB() {
SQLiteDatabase myDb;
String MySQL;
byte[] byteImage1 = null;
byte[] byteImage2 = null;
MySQL = "create table emp1(_id INTEGER primary key autoincrement, sample TEXT not null, audio BLOB);";
myDb = openOrCreateDatabase("Blob List", Context.MODE_PRIVATE, null);
myDb.execSQL(MySQL);
String s = myDb.getPath();
textView.append("\r\n" + s + "\r\n");
myDb.execSQL("delete from emp1");
ContentValues newValues = new ContentValues();
newValues.put("sample", "HI Hello");
try {
InputStream is = new FileInputStream("YOUR IMAGE PATH");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
textView.append("\r\n" + bytes.length + "\r\n");
newValues.put("audio", bytes);
long ret = myDb.insert("emp1", null, newValues);
if (ret < 0)
textView.append("\r\n!!! Error add blob failed!!!\r\n");
} catch (IOException e) {
textView.append("\r\n!!! Error: " + e + "!!!\r\n");
}
Cursor cur = myDb.query("emp1", null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
textView.append("\r\n" + cur.getString(1) + "\r\n");
cur.moveToNext();
}
// /////Read data from blob field////////////////////
cur.moveToFirst();
byteImage2 = cur.getBlob(cur.getColumnIndex("audio"));
// bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0,
// byteImage2.length));
textView.append("\r\n" + byteImage2.length + "\r\n");
cur.close();
myDb.close();
}