Sit*_*ten 73 android blob image
我想将图像(从url)存储到sqlite数据库中.
为此,我使用:
db = new DataBase(getApplicationContext());
URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
barb.append((byte) current);
}
ContentValues filedata= new ContentValues();
filedata.put(DataBase.IMG_SRC,barb.toByteArray());
db.insert(DataBase.Table_Img, null, filedata);
Run Code Online (Sandbox Code Playgroud)
在Insert():
public void insert(String tableImg, Object object,
ContentValues dataToInsert) {
// TODO Auto-generated method stub
String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " +
"VALUES ('"+1+"','"+dataToInsert+"')";
db.execSQL(sql);
}
Run Code Online (Sandbox Code Playgroud)
用于检索图像:
Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC);
byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC));
cursor.close();
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
System.out.println(">>>>>>>>>>>>>>>>>>>>>> "+theImage);
Run Code Online (Sandbox Code Playgroud)
所以我得到了null.
在我的数据库中,图像的值存储为: Image=[B@43e5ac48]
ble*_*enm 65
这里是我用于我的应用程序的代码
此代码将从url获取图像并将其转换为字节数组
byte[] logoImage = getLogoImage(IMAGEURL);
private byte[] getLogoImage(String url){
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
要将图像保存到db,我使用了此代码.
public void insertUser(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String delSql = "DELETE FROM ACCOUNTS";
SQLiteStatement delStmt = db.compileStatement(delSql);
delStmt.execute();
String sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)";
SQLiteStatement insertStmt = db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Integer.toString(this.accId));
insertStmt.bindString(2,this.accName);
insertStmt.bindBlob(3, this.accImage);
insertStmt.executeInsert();
db.close();
}
Run Code Online (Sandbox Code Playgroud)
要检索图像,这是我使用的代码.
public Account getCurrentAccount() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "SELECT * FROM ACCOUNTS";
Cursor cursor = db.rawQuery(sql, new String[] {});
if(cursor.moveToFirst()){
this.accId = cursor.getInt(0);
this.accName = cursor.getString(1);
this.accImage = cursor.getBlob(2);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
if(cursor.getCount() == 0){
return null;
} else {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
最后将此图像加载到imageview
logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage,
0,currentAccount.accImage.length));
Run Code Online (Sandbox Code Playgroud)
Bal*_*i.K 24
在DBAdaper即Data Base助手类中声明这样的表
private static final String USERDETAILS=
"create table userdetails(usersno integer primary key autoincrement,userid text not null ,username text not null,password text not null,photo BLOB,visibility text not null);";
Run Code Online (Sandbox Code Playgroud)
插入这样的值,
首先将图像转换为byte []
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.common)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] photo = baos.toByteArray();
db.insertUserDetails(value1,value2, value3, photo,value2);
Run Code Online (Sandbox Code Playgroud)
在DEAdaper类中
public long insertUserDetails(String uname,String userid, String pass, byte[] photo,String visibility)
{
ContentValues initialValues = new ContentValues();
initialValues.put("username", uname);
initialValues.put("userid",userid);
initialValues.put("password", pass);
initialValues.put("photo",photo);
initialValues.put("visibility",visibility);
return db.insert("userdetails", null, initialValues);
}
Run Code Online (Sandbox Code Playgroud)
检索图像如下
Cursor cur=your query;
while(cur.moveToNext())
{
byte[] photo=cur.getBlob(index of blob cloumn);
}
Run Code Online (Sandbox Code Playgroud)
将byte []转换为图像
ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
Run Code Online (Sandbox Code Playgroud)
我认为这个内容可以解决您的问题
| 归档时间: |
|
| 查看次数: |
182206 次 |
| 最近记录: |