导入包名
implementation 'com.github.bumptech.glide:glide:3.8.0'
相册布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/header"
android:fitsSystemWindows="true"
android:background="@color/colorPrimary"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:text="选择相册"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textColor="@color/white"
android:textSize="17sp"
android:id="@+id/txt_toname"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="?attr/actionBarSize"
android:scaleType="fitCenter"
android:id="@+id/btn_back"
android:layout_alignParentBottom="true"
android:background="@color/transparent"
android:src="@drawable/backer"
android:padding="18dp"/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:id="@+id/lst"
android:scrollbars="none"
android:layout_height="match_parent">
</ListView>
<ViewStub
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/loading"
android:id="@+id/loading"/>
</LinearLayout>
相册条目布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="94dp"
android:layout_margin="3dp"
android:layout_height="94dp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:id="@+id/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/colorPrimary"
android:layout_toRightOf="@id/img"
android:textSize="15sp"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:gravity="center_vertical"
android:id="@+id/path"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="@color/colorPrimary"
android:textSize="13sp"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:layout_toRightOf="@id/img"
android:layout_below="@id/path"
android:gravity="center_vertical"
android:id="@+id/size"/>
</RelativeLayout>
图片页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/header"
android:fitsSystemWindows="true"
android:background="@color/colorPrimary"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:text="选择图片"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textColor="@color/white"
android:textSize="17sp"
android:id="@+id/txt_toname"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="?attr/actionBarSize"
android:scaleType="fitCenter"
android:id="@+id/btn_back"
android:layout_alignParentBottom="true"
android:background="@color/transparent"
android:src="@drawable/backer"
android:padding="18dp"/>
</RelativeLayout>
<GridView
android:layout_width="match_parent"
android:id="@+id/grid"
android:numColumns="4"
android:scrollbars="none"
android:layout_below="@id/header"
android:layout_height="match_parent">
</GridView>
</RelativeLayout>
图片条目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_margin="3dp"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:id="@+id/img"/>
</LinearLayout>
相册页面代码
public class ChooseAlbumActivity extends Activity {
private ListView lst;
private HashMap<String, List<String>> albums;
private MyAdapter myAdapter;
private ViewStub loading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mine_choose_album);
loading = findViewById(R.id.loading);
lst = findViewById(R.id.lst);
lst.setAdapter(myAdapter = new MyAdapter());
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ChooseAlbumActivity.this, ChoosePhotoActivity.class);
intent.putExtra("photos", (Serializable) albums.get(albums.keySet().toArray()[position]));
intent.putExtra("sign", getIntent().getIntExtra("sign", -1));
startActivity(intent);
}
});
findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
if (albums == null)
return 0;
return albums.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null){
convertView = LayoutInflater.from(ChooseAlbumActivity.this).inflate(R.layout.own_album_item, null, false);
viewHolder = new ViewHolder();
viewHolder.path = convertView.findViewById(R.id.path);
viewHolder.img = convertView.findViewById(R.id.img);
viewHolder.size = convertView.findViewById(R.id.size);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.size.setText("此相册共有:" + albums.get(albums.keySet().toArray()[position]).size() + "张");
viewHolder.path.setText(((String)albums.keySet().toArray()[position]).replace(Environment.getExternalStorageDirectory().getPath() + "/", ""));
MyImgShow.showLocalImgSquare(ChooseAlbumActivity.this, "file://" + albums.get(albums.keySet().toArray()[position]).get(0), viewHolder.img);
return convertView;
}
public class ViewHolder {
TextView path;
ImageView img;
TextView size;
}
}
@Override
protected void onResume() {
super.onResume();
new WeakTask<Integer>(new MyWeakTaskListener()).execute();
}
private HashMap<String, List<String>> getSystemPhotoList()
{
HashMap<String, List<String>> temps = new HashMap<String, List<String>>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null || cursor.getCount() <= 0)
return temps; // 没有图片
while (cursor.moveToNext())
{
int index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String path = cursor.getString(index); // 文件地址
File file = new File(path);
if (file.exists())
{
String fatherPath = path.substring(0, path.lastIndexOf('/'));
if (temps.get(fatherPath) == null){
temps.put(fatherPath, new ArrayList<String>());
}
temps.get(fatherPath).add(path);
}
}
Object[] keys = temps.keySet().toArray();
for (int i = 0; i < keys.length; i++){
Collections.reverse(temps.get(keys[i]));
}
return temps;
}
private class MyWeakTaskListener implements OnWeakTaskListener<Integer>{
@Override
public void before() {
loading.setVisibility(View.VISIBLE);
}
@Override
public Integer middle() {
albums = getSystemPhotoList();
return 2;
}
@Override
public void after(Integer integer) {
loading.setVisibility(View.GONE);
myAdapter.notifyDataSetChanged();
}
}
}
图片页面代码
public class ChoosePhotoActivity extends Activity {
private GridView grid;
private List<String> photos;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mine_choose_photo);
grid = findViewById(R.id.grid);
photos = (List<String>) getIntent().getSerializableExtra("photos");
grid.setAdapter(new MyAdapter());
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//在这里添加事件
Intent intent = new Intent(ChoosePhotoActivity.this, ChooseResultActivity.class);
startActivity(intent.putExtra("path", photos.get(position)));
}
});
findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
if (photos == null)
return 0;
return photos.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(ChoosePhotoActivity.this).inflate(R.layout.own_photo_item, grid, false);
viewHolder = new ViewHolder();
viewHolder.img = convertView.findViewById(R.id.img);
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
MyImgShow.showLocalImgSquare(ChoosePhotoActivity.this, "file://" + photos.get(position), viewHolder.img);
return convertView;
}
public class ViewHolder {
ImageView img;
}
}
}
辅助类
MyImageShow
public class MyImgShow {
public static void showLocalImgSquare(Context context, String url, ImageView img){
Glide.with(context)
.load(url)
.asBitmap()
.error(R.drawable.error)
.placeholder(R.drawable.placehoder)
.dontAnimate()
.override(200, 200)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.fitCenter()
.into(img);
}
}
WeakTask
网友评论