[英]Get images from external storage and display them in a Gallery
I've created a folder called "TopClothes" and I have some photos there. 我已经创建了一个名为“ TopClothes”的文件夹,并且那里有一些照片。 It is store in my SD Card. 它存储在我的SD卡中。 I've been looking for an answer but can't seem to find one. 我一直在寻找答案,但似乎找不到答案。 I just want to be able to "read" those images in the folder and display them in a Gallery in my app. 我只希望能够“读取”文件夹中的图像并将其显示在我的应用程序的“图库”中。 Is there a way of doing this? 有办法吗? Here's some of what I have on my MainActivity
: 这是我在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);
}
The image is set correctly, but its only 1 image, and I want to have lots of images and be able to scroll and see them. 该图像设置正确,但只有1张图像,我想拥有很多图像并能够滚动并查看它们。
This is my ArrayAdapter
: 这是我的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;
}
Notice that public Integer[] ImgIds = {};
注意public Integer[] ImgIds = {};
is empty. 是空的。 Should the file path be there? 文件路径应该在那里吗?
There are a few different issues with your current implementation. 当前的实现存在一些不同的问题。
You are opening files in many places, sometimes using the results, sometimes not using the results at all, and sometimes only using a small subset of the results. 您正在许多地方打开文件,有时使用结果,有时根本不使用结果,有时仅使用结果的一小部分。
In your first code sample, you retrieve the entire contents of the "TopClothes" file, but only place a single image file into the ImageView you have declared. 在您的第一个代码示例中,您将检索“ TopClothes”文件的全部内容,但只能将单个图像文件放入您声明的ImageView中。 This image will be the last image retrieved from the file. 该图像将是从文件中检索到的最后一个图像。
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);
}
A second problem related to accessing storage is how your Adapter is retrieving data directly from the storage when creating the view to be returned. 与访问存储有关的第二个问题是,当创建要返回的视图时,适配器如何直接从存储中检索数据。 You should probably retrieve that image data once, and then pass the entire image data list into the adapter for use. 您可能应该一次检索该图像数据,然后将整个图像数据列表传递到适配器中以供使用。 As implemented, you are retrieving the entire contents of this "TopClothing" directory for each and every ImageView in the Gallery, then choosing a single image from the data to put into an ImageView. 实施后,您将为Gallery中的每个ImageView检索此“ TopClothing”目录的全部内容,然后从数据中选择一个图像放入ImageView中。
This is a lot of unnecessary overhead 这是很多不必要的开销
This does not use the results retrieved from storage to create each view. 这不会使用从存储中检索到的结果来创建每个视图。
@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.