[英]Android ,What Is The Best way to store image to SQLite database , and retreive image from db?
[英]Android - What's the best way to store image to SQLite?
我正在開發一個脫機應用程序,但沒有從用戶的畫廊獲得任何圖片。 但是我想向手動插入的用戶顯示圖像。
我能想到的唯一解決方案是將這些圖像放入drawable
,然后將其保存到sqlite數據庫(我不確定如何)。 能做到嗎?
謝謝。
如果您願意將圖像存儲在可繪制的文件夾中,則可以在數據庫中存儲對其的引用。 只需存儲com.example.yourapp:drawable/imagefilename
類的字符串,然后使用它來顯示圖像,其中yourString
包含數據庫中的字符串。
int imageResource = this.getResources().getIdentifier(yourString, null, null);
yourimageview.setImageResource(imageResource);
您可以為此使用insert blob
public void insertImg(int id , Bitmap img ) {
byte[] data = getBitmapAsByteArray(img); // this is a function
insertStatement_logo.bindLong(1, id);
insertStatement_logo.bindBlob(2, data);
insertStatement_logo.executeInsert();
insertStatement_logo.clearBindings() ;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.