简体   繁体   English

从外部存储获取图像并将其显示在图库中

[英]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. 当前的实现存在一些不同的问题。

  1. Storage access should be minimized when possible. 在可能的情况下,应尽量减少对存储的访问。

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 这是很多不必要的开销

  1. The Adapter does not actually put an image into the views that it returns. 适配器实际上不会将图像放入返回的视图中。

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.

相关问题 Android:从图库中获取图像,在回收器适配器中显示它们 - Android: get images from gallery, display them in recycler adapter 如何从图库中获取图像并将其显示在android SDK中的屏幕上 - How to get images from gallery and display them to the screen in android sdk Android - 从 url 获取图像作为 JSON 并在画廊视图中显示它们 - Android - Get Images from url as JSON and display them in Gallery view 如何从URL中检索多个图像并将其显示在Android的图库中? - How to retrieve mutiple images from url and display them in a gallery in android? 如何从外部存储获取图像 - How get images from external storage 从图库中获取图像的可绘制地址,并将其存储在数组中 - Get images' drawable addresses from the gallery and store them inside an array 如何从图库中隐藏FilePicker图像? 以及如何将这些图像存储在外部存储器的文件夹中? - How to Hide FilePicker images from Gallery? and How to store those images in a folder inside external storage? Oreo ViewPager显示来自外部存储文件路径的图像 - Oreo ViewPager to display images from external storage filepaths 如何在颤振中显示来自外部存储路径的图像? - How to display images from a external storage path in flutter? 如何为画廊中的多个图像传递图像数组并将其显示在Flutter中的另一个小部件上 - How to pass an array of images for selected multiple images from gallery and display them on another widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM