2012-11-29 90 views
1

我正在開發一個離線應用程序,我沒有從用戶的畫廊得到任何圖片。 但我想將圖像顯示給我手動插入的用戶。Android - 將圖像存儲到SQLite的最佳方式是什麼?

我能想到的唯一解決方案是將這些圖像放入drawable,然後將其保存到sqlite數據庫(我不知道如何)。這可以做到嗎?

謝謝。

+1

你不能(容易)將文件添加到可繪製文件夾。您必須將它們保存到您可以創建的SD卡中的應用程序文件夾中。您可能還希望查看內容提供商以增加功能。 http://developer.android.com/guide/topics/providers/content-provider-creating.html – DeeV

+0

我明白了。如果應用程序是在線應用程序呢? –

+0

如果您要將圖像下載到設備上,則是一樣的。如果你只是顯示一個頁面,那麼你可能想看看WebView。 – DeeV

回答

1

如果您願意將圖像存儲在可繪製文件夾中,則可以在數據庫中存儲對它們的引用。只存儲一個字符串像com.example.yourapp:drawable/imagefilename,並以此來顯示圖像,其中yourString包含數據庫字符串..

int imageResource = this.getResources().getIdentifier(yourString, null, null); 
yourimageview.setImageResource(imageResource); 
0

您可以使用插入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(); 
} 
相關問題