Android实现获取手机里面的所有图片

public class MainActivity extends Activity {
//查看图片按钮
    private Button look;
    private Button add;
//显示图片名称的list
    ListView show_list;
    ArrayList names = null;
    ArrayList descs= null;
    ArrayList fileNames = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        look = (Button) findViewById(R.id.look);
        add = (Button) findViewById(R.id.add);
        show_list = (ListView) findViewById(R.id.show_list);
        look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                names = new ArrayList();
                descs = new ArrayList();
                fileNames = new ArrayList();
                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));
                    //获取图片的生成日期
                    byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    //获取图片的详细信息
                    String desc = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION));
                    names.add(name);
                    descs.add(desc);
                    fileNames.add(new String(data, 0, data.length - 1));
                }
                List<Map<String, Object>> listItems = new ArrayList<>();
                for (int i = 0; i < names.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", names.get(i));
                    map.put("desc", descs.get(i));
                    listItems.add(map);
                }
                //设置adapter
                SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, listItems,
                        R.layout.line, new String[]{"name", "desc"}, new int[]{R.id.name, R.id.desc});

                show_list.setAdapter(adapter);
            }
        });

        ///list的点击事件
    show_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            View viewDiag = getLayoutInflater().inflate(R.layout.view,null);
            ImageView image = (ImageView)viewDiag.findViewById(R.id.image);
            image.setImageBitmap(BitmapFactory.decodeFile((String) fileNames.get(i)));
            new AlertDialog.Builder(MainActivity.this).setView(viewDiag)
                    .setPositiveButton("确定",null).show();
        }
    });
    }
}


<!--获取手机存储卡权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值