2014-11-02 118 views
0

我創建了一個名爲「TopClothes」的文件夾,並且我在那裏有一些照片。它存儲在我的SD卡中。我一直在尋找答案,但似乎無法找到答案。我只是想能夠「讀取」文件夾中的這些圖像,並將它們顯示在我的應用程序中的圖庫中。有沒有辦法做到這一點? 下面是一些什麼,我有我的MainActivity從外部存儲器獲取圖像並將其顯示在圖庫中

gal = (Gallery)findViewById(R.id.gallery1); 
    gal.setAdapter(new ImageAdapter(this)); 
    iv = (ImageView)findViewById(R.id.imgView); 

File file = new File(Environment.getExternalStorageDirectory()+"/TopClothes/"); 
    File imageList[] = file.listFiles(); 

    for(int i=0;i<imageList.length;i++) 
    { 
     Log.e("Image: "+i+": path", imageList[i].getAbsolutePath()); 
     Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath()); 
     iv.setImageBitmap(b); 
    } 

圖像設置正確,但它只有1形象,我想有大量的圖片,並能夠滾動看到他們。

這是我ArrayAdapter

private Context context; 
public Integer[] ImgIds = {}; 

public ImageAdapter(Context context) { 
    // TODO Auto-generated constructor stub 
    this.context = context; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return ImgIds.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    ImageView imgView = null; 
    if(convertView == null){ 
     imgView = new ImageView(context); 
     imgView.setLayoutParams(new Gallery.LayoutParams(300, 500)); 
    }else { 
     imgView = (ImageView)convertView; 
    } 

    File file = new File(Environment.getExternalStorageDirectory()+"/TopClothes/"); 
    File imageList[] = file.listFiles(); 

    imgView.setImageResource(ImgIds[position]); 
    return imgView; 
} 

注意public Integer[] ImgIds = {};是空的。文件路徑應該在那裏嗎?

+0

爲什麼不用'setType'的'Intent.ACTION_PICK'作爲圖像? – KOTIOS 2014-11-02 05:08:00

回答

0

您當前的實施有幾個不同的問題。

  1. 存儲訪問應儘可能最小化。

您正在許多地方打開文件,有時使用結果,有時根本不使用結果,有時僅使用一小部分結果。

在您的第一個代碼示例中,您將檢索「TopClothes」文件的全部內容,但只將一個圖像文件放入您聲明的ImageView中。該圖像將是從文件中檢索的最後一張圖像。

gal = (Gallery)findViewById(R.id.gallery1); 
gal.setAdapter(new ImageAdapter(this)); 
iv = (ImageView)findViewById(R.id.imgView); 

// This File open could be reduced into a single retrieval for the image 
// which will be used in the ImageView. 

File file = new File(Environment.getExternalStorageDirectory()+"/TopClothes/"); 
File imageList[] = file.listFiles(); 

// This entire for loop could be removed and replaced with a single Bitmap creation and 
// assignment into the ImageView. 

for(int i=0;i<imageList.length;i++) 
{ 
    Log.e("Image: "+i+": path", imageList[i].getAbsolutePath()); 
    Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath()); 
    iv.setImageBitmap(b); 
} 

與訪問存儲的第二個問題是如何您的適配器直接從存儲創建視圖時要返回檢索數據。您應該一次檢索該圖像數據,然後將整個圖像數據列表傳遞到適配器中以供使用。正如所實現的那樣,您正在檢索Gallery中每個ImageView的「TopClothing」目錄的全部內容,然後從數據中選擇一個圖像放入ImageView中。

這是很多不必要的開銷的

  • 適配器實際上不把一個圖像劃分成的觀點,即它返回。
  • 這不使用從存儲檢索的結果來創建每個視圖。

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        ImageView imgView = null; 
        if(convertView == null){ 
         imgView = new ImageView(context); 
         imgView.setLayoutParams(new Gallery.LayoutParams(300, 500)); 
        }else { 
         imgView = (ImageView)convertView; 
        } 
    
        File file = new File(Environment.getExternalStorageDirectory()+"/TopClothes/"); 
    
        // ImgIds should either be filled with the data when loaded from this directory. 
        // Rather than 'imageList'. 
        File imageList[] = file.listFiles(); 
    
        // OR This should give the ImageView an image from the 'imageList' which was actually filled. 
        imgView.setImageResource(ImgIds[position]); 
        return imgView; 
    } 
    
    +0

    但這只是顯示1圖像?因爲ImageView只是測試它是否工作。我正在考慮將ImageView移除到只包含TopClothes文件夾中所有圖像的圖庫。 – GUZ 2014-11-02 15:31:21

    相關問題