2015-09-16 188 views
-5

嗨,我是Android新手。可以某人描述如何存儲圖像到Android數據庫&檢索它嗎?Android數據庫存儲和檢索

+3

想想你自己很幸運,你不知道如何將圖像保存到數據庫。 IT是一個非常糟糕的做法。只需插入鏈接或文件路徑。 – e4c5

+0

文件或任何二進制數據應該很少存儲在數據庫中。它們應該由文件系統存儲,存儲在數據庫中的文件的路徑。長話短說,關係型/ SQL數據庫根本無法很好地處理這類數據,並且被認爲是一種行業反作用,不僅在Android中這樣做,而且在整體編程中也是如此。 –

+0

'可以一些人描述的方式... ...這不是一個教程網站, –

回答

0

看看這個教程...

http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

,你可以使用此代碼:
用於存儲圖像:

Map<String, byte[]> hh = new HashMap<String, byte[]>(); 

String hi = "http://i.stack.imgur.com/TLjuP.jpg"; 
byte[] logoImagedata = getLogoImage(hi);  

hh.put("img",logoImagedata); 

爲retriving:

byte[] imageByteArray = hh.get("img"); 

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray); 
Bitmap theImage= BitmapFactory.decodeStream(imageStream); 

和getLogoImage:

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; 
     } 

    } 

我希望這對你有所幫助。

+0

謝謝你這麼多爲你的幫助:) – Anu

+0

如果這個答案幫助你解決你的問題,那麼接受爲其他人的答案。 – zheek

相關問題