Android如何设置Image本地存储路径为ListView
How to set Image local storage path to ListView in Android
我的 CustomList 适配器代码...
private final Activity context;
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: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163055.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163037.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170217_175804.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170217_175802.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163056.jpg
现在我想使用自定义适配器或使用 ArrayAdapter 显示列表视图的这些路径。
假设您要发送一个路径数组列表到您的自定义适配器。现在在自定义适配器的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);
}
如果您需要有关自定义适配器的帮助,请告诉我
注意:如果你有很多图片,我建议你使用像Picasso or Glide
这样的库
不要在 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;
我的 CustomList 适配器代码...
private final Activity context;
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: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163055.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163037.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170217_175804.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170217_175802.jpg
d: 数组 path/storage/sdcard/DCIM/Camera/IMG_20170210_163056.jpg
现在我想使用自定义适配器或使用 ArrayAdapter 显示列表视图的这些路径。
假设您要发送一个路径数组列表到您的自定义适配器。现在在自定义适配器的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);
}
如果您需要有关自定义适配器的帮助,请告诉我
注意:如果你有很多图片,我建议你使用像Picasso or Glide
这样的库不要在 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;