安卓专业高级一Day11

丶灬走出姿态 提交于 2020-03-04 22:11:46

ContentProvider 制作一个图片浏览器

布局

<?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"
    android:orientation="vertical"
    tools:context=".Main2Activity">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查看所有图片" />

    <GridView
        android:id="@+id/gv_id"
        android:numColumns="3"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </GridView>
</LinearLayout>

代码

package com.example.day011;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2Activity extends AppCompatActivity {

    private static final String TAG = "Main2Activity";
    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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button = (Button) findViewById(R.id.button1);
        gvId = findViewById(R.id.gv_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Log.i(TAG, "onClick: ");
                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文件布局代表的视图
                View view = getLayoutInflater().inflate(R.layout.view, null);
                // 获取图片组件
                ImageView image = (ImageView) view.findViewById(R.id.imageView1);
                image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(arg2)));
                // 显示图片
                new AlertDialog.Builder(Main2Activity.this).setView(view).setPositiveButton("确定", null).show();
            }
        });

    }

    @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(Main2Activity.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(Main2Activity.this).load(s).into(viewHolder.imageView);
            return convertView;
        }
        class ViewHolder{
            ImageView imageView;
        }
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!