我创建了一个名为" 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 :(得分:0)
您当前的实施存在一些不同的问题。
您正在许多地方打开文件,有时使用结果,有时根本不使用结果,有时只使用结果的一小部分。
在第一个代码示例中,您将检索“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;
}