超簡單圖片瀏覽器
首先展示一下簡單的效果
1缸濒、manifest清單文件中的授權(quán),獲取讀寫權(quán)限录择,展示手機(jī)(模擬器)中所有的圖片
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
build.gradle中Glide版本
implementation 'com.github.bumptech.glide:glide:4.5.0'
2寄纵、xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="10dp">
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看圖片"
android:background="@android:color/holo_blue_bright"/>
<GridView
android:id="@+id/gv_id"
android:numColumns="4"
android:gravity="center"
android:horizontalSpacing="5dp"
android:verticalSpacing="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
</LinearLayout>
3、網(wǎng)格布局顯示圖片的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/im_id"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
4癣籽、彈框布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
5瀑踢、MainActivity中的方法
public class MainActivity extends AppCompatActivity {
private Button button;
private List<String> names = new ArrayList<String>();
private List<String> descs = new ArrayList<String>();
private List<String> fileNames = new ArrayList<String>();
private GridView gvId;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button_1);
gvId = findViewById(R.id.gv_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
}
});
gvId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 加載view.xml文件布局代表的視圖
final View view = getLayoutInflater().inflate(R.layout.img_view_itme, null);
// 獲取圖片組件
ImageView image = (ImageView) view.findViewById(R.id.imageView1);
image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(arg2)));
// AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// builder.setView(view);
//
// AlertDialog alertDialog = builder.create();
// alertDialog.show();
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.setMessage("加載中...");
dialog.show();//顯示
//閑著沒事模擬一個進(jìn)度條
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress >= 100){
dialog.dismiss();
timer.cancel();
handler.post(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view).setCancelable(true);
//使用建造者創(chuàng)建對話框
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
dialog.setProgress(progress += 2);
}
},0,10);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
allPic();
}
}
private void allPic() {
names.clear();
descs.clear();
fileNames.clear();
// 通過ContentProvider查詢所有的圖片信息
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
// 獲取圖片的顯示名
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// 將圖片描述添加到集合中
descs.add(desc);
names.add(name);
fileNames.add(new String(data, 0, data.length - 1));
}
gvId.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return fileNames.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.image_layout,parent,false);
viewHolder = new ViewHolder();
viewHolder.imageView = convertView.findViewById(R.id.im_id);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
String s = fileNames.get(position);
Glide.with(MainActivity.this).load(s).into(viewHolder.imageView);
return convertView;
}
class ViewHolder{
ImageView imageView;
}
}
}