簡體   English   中英

Android-將圖像存儲到SQLite的最佳方法是什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM