我創建了一個名爲「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 = {};
是空的。文件路徑應該在那裏嗎?
爲什麼不用'setType'的'Intent.ACTION_PICK'作爲圖像? – KOTIOS 2014-11-02 05:08:00