Android 实现获取手机里面的所有图片详解及实例
程序员文章站
2023-11-17 09:29:46
android 实现获取手机里面的所有图片详解及实例
实现代码:
public class mainactivity extends activity {...
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(); } }); } }
xml:
<!--获取手机存储卡权限--> <uses-permission android:name="android.permission.read_external_storage"/>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!