[英]Get images from external storage and display them in a Gallery
我已經創建了一個名為“ 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 = {};
是空的。 文件路徑應該在那里嗎?
當前的實現存在一些不同的問題。
您正在許多地方打開文件,有時使用結果,有時根本不使用結果,有時僅使用結果的一小部分。
在您的第一個代碼示例中,您將檢索“ 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.