如何在android中设置ImageView的本地存储路径

时间:2017-04-17 06:05:11

标签: android listview android-studio custom-lists android-storage

enter image description here

My CustomList AdapterCode ...

私人最终活动背景;

private List<String> list;

public CustomListAdapter(Activity context, List<String> list) {
    super(context, R.layout.list_single);

    this.context = context;
    this.list = list;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_single, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


    if (list != null) {
        for (int i = 0; i < list.size(); i++) {

            File imgFile = new File(list.get(i));

            if (imgFile.exists()) {

                ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                imageView.setImageBitmap(myBitmap);
            }
        }
    }

    return rowView;
}

@Override
public int getCount() {
    return list.size();
}

}

我正在接收像

这样的imagePath列表

d:阵列路径/存储/ sdcard / DCIM / Camera / IMG_20170210_163055.jpg

d:阵列路径/存储/ sdcard / DCIM / Camera / IMG_20170210_163037.jpg

d:阵列路径/存储/ sdcard / DCIM / Camera / IMG_20170217_175804.jpg

d:阵列路径/存储/ sdcard / DCIM / Camera / IMG_20170217_175802.jpg

d:阵列路径/存储/ sdcard / DCIM / Camera / IMG_20170210_163056.jpg

现在,我想使用自定义适配器或使用ArrayAdapter显示列表视图的这些路径。

1 个答案:

答案 0 :(得分:1)

假设您正在发送自定义适配器的路径数组列表。现在,在自定义适配器的getView()方法中,添加以下内容:

File imgFile = new  File(myList.get(position));

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) convertView.findViewById(R.id.imageviewTest);
    //the imageview of your custom list item

    myImage.setImageBitmap(myBitmap);

}

如果您需要有关自定义适配器的帮助,请告诉我们

注意:如果您有大量图片,建议您使用PicassoGlide

等库

不要在getview中使用for循环。每次创建行时都会调用GetView。

View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


if (list != null) {

        File imgFile = new File(list.get(position));

        if (imgFile.exists()) {

            ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imageView.setImageBitmap(myBitmap);
        }
}
return rowView;