Picture viewer

超简单图片浏览器

首先展示一下简单的效果
就是这样

1、manifest清单文件中的授权,获取读写权限,展示手机(模拟器)中所有的图片

    
    

build.gradle中Glide版本

implementation 'com.github.bumptech.glide:glide:4.5.0'

2、xml布局




    
布局样式

3、网格布局显示图片的布局




    


4、弹框布局




    


5、MainActivity中的方法

public class MainActivity extends AppCompatActivity {

    private Button button;
    private List names = new ArrayList();
    private List descs = new ArrayList();
    private List fileNames = new ArrayList();
    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();//显示
                //闲着没事模拟一个进度条
                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);
                                    //使用建造者创建对话框
                                    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;
        }
    }
}

你可能感兴趣的:(Picture viewer)