今天我们学习如何在我们的程序显示相册图片。
首先创建一个 Image类来存储图片信息:
public class Image {
private int id;
private String data;//path
private int size;
private String displayName;
private long dateTaken;
private int width;
private int height;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public long getDateTaken() {
return dateTaken;
}
public void setDateTaken(long dateTaken) {
this.dateTaken = dateTaken;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
定义一个接口IDao,定义获取图片数据的方法:
public interface IDao<T> {
List<T> getData();
}
创建一个 ImageDao继承 IDao,来具体实现获取图片的方法:
public class ImageDao implements IDao<Image> {
private Context context;
public ImageDao(Context context) {
this.context = context;
}
@Override
public List<Image> getData() {
//准备返回值
List<Image> images = new ArrayList<Image>();
//准备ContentResolver
ContentResolver cr = context.getContentResolver();
//准备Uri
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//执行查询,获取Cursor
//注意和数据库保持一致
String[] projection = {
"_id",//0
"_data",//1
"_size",//2
"_display_name",//3
"datetaken",//4
"width",//5
"height",//6
};
Cursor c = cr.query(uri, projection, null, null, null);
//遍历Cursor,读取数据,并封装到返回值
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
Image image = new Image();
image.setId(c.getInt(0));
image.setData(c.getString(1));
image.setSize(c.getInt(2));
image.setDisplayName(c.getString(3));
image.setDateTaken(c.getLong(4));
image.setWidth(c.getInt(5));
image.setHeight(c.getInt(6));
images.add(image);
}
//释放资源
c.close();
c = null;
//日志
for (Image image : images) {
Log.d("Image", "id:" + image.getId() + ";data:" + image.getData() + ";size:" + image.getSize() +
";displayname:" + image.getDisplayName() + ";dataTaken:" + image.getDateTaken() + ";width:" + image.getWidth() +
";height:" + image.getHeight());
}
//返回
return images;
}
}
准备 BaseAdapter,可以从之前文章中拿过来:传送门
写一个 ImageAdapter 继承 BaseAdapter
public class ImageAdapter extends BaseAdapter<Image> {
public ImageAdapter(Context context, List data) {
super(context, data);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = getInflater().inflate(R.layout.item_image, null);
viewHolder.imageView = view.findViewById(R.id.img_item);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Image image = getData().get(i);
Bitmap bitmap = BitmapFactory.decodeFile(image.getData());
viewHolder.imageView.setImageBitmap(bitmap);
return view;
}
class ViewHolder {
ImageView imageView;
}
}
准备 ListView 的 item 布局:item_image
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/img_item"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView listView;
private ImageAdapter imageAdapter;
private List<Image> imageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.lv_images);
imageList = new ImageDao(this).getData();
imageAdapter = new ImageAdapter(this, imageList);
listView.setAdapter(imageAdapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
}
}
}
MainActivity 的布局:activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_images"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
AndroidManifest中增加读取存储空间的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
记得手机打开相应权限
运行程序:
如果想实现一行显示 3 个,可以把 ListView 控件改为 GridView
<GridView
android:id="@+id/lv_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:horizontalSpacing="9dp"
android:numColumns="3"
android:verticalSpacing="10dp" />
运行程序: