2012-10-03 43 views
3

我使用圖像滑塊在我的佈局,爲此,我使用轉換位圖繪製,並將其存儲在陣列

 private Integer[] mImageIds = { R.drawable.j, R.drawable.f,R.drawable.i, R.drawable.g, R.drawable.k }; 

採取images.But現在我想從SD卡拍攝的圖像和存儲這些圖像進入該陣列。我可以如何做到這一點?下面是 是我的代碼。我存儲在數據庫中的拇指。我將位圖轉換爲drawable,然後如何將其存儲到Array mImages中?

 List<String> thumb = new ArrayList<String>(); 
    public Integer[] mImageIds; 
    Bitmap bitmap; 
      thumb = db.getRecomdThumb();//get image address from db 
    for(int i = 0; i < thumb.size();i++){ 
     bitmap = BitmapFactory.decodeFile(thumb.get(i)); 
     Drawable d = new BitmapDrawable(getResources(),bitmap); 

    } 
+0

我想只顯示從SD卡的一些圖像,「M與數據庫交互 –

回答

1

只有存儲在「res」文件夾內的圖像可以添加到整數數組中。您可以從動態創建的位圖創建一個int res。您只能將其轉換爲Drawable,如您在代碼中所述。

如果你問我,如果我將動態創建的drawable添加到res文件夾,是否有可能獲得Int表示形式,這是不可能的。

「Res」文件夾無法修改。它是隻讀的。

所以你要做的是,只需創建一個Drawable數組而不是Integer數組並執行操作。

像這樣的工作,

List<String> thumb = new ArrayList<String>(); 
    public Integer[] mImageIds; 
    Bitmap bitmap; 
      thumb = db.getRecomdThumb();//get image address from db 
    private Drawable[] drawables = new Drawable[thumb.size()]; 
    for(int i = 0; i < thumb.size();i++){ 
     bitmap = BitmapFactory.decodeFile(thumb.get(i)); 
     drawables[i] = new BitmapDrawable(getResources(),bitmap); 

    } 
+1

謝謝你的工作對我來說.. –

+0

你是最歡迎的Brinda .. –

0

你可以只創建一個

private Drawable[] drawables = new Drawable[mImageIds.length]; 

,並在各自的尺寸複製影像。

話雖如此:drawables可能很大,並將它們(或位圖)存儲在內存中可能會使您快速遇到內存不足的情況,因爲手機通常具有小堆大小。

相關問題