GridView实现加载本地所有图片

news/2024/7/18 8:01:08
public class MainActivity extends Activity {
private GridView gridView;
// 获取所有图片的路径
List<String> imagePath = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
String sdpath=Environment.getExternalStorageDirectory()+"/";
getFiles(sdpath);
if(imagePath.size()<1){
return;
}
final MyAdapter adapter = new MyAdapter();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imagePath.remove(position);
adapter.notifyDataSetChanged();

}
});
}


// 获取所有图片格式
private static final String[] imageFromSet = new String[] { "jpg", "png",
"gif" };


// 判断是否是图片
private static boolean isImageFile(String path) {
for (String imagePath : imageFromSet) {
if (path.contains(imagePath)) {
return true;
}
}
return false;
}




//遍历指定路径
private void getFiles(String url){
//创建文件对象
File files=new File(url);
File[] file=files.listFiles();
try {
for(File f:file){
if(f.isDirectory()){//如果是目录
getFiles(f.getAbsolutePath());//递归调用
}else{
if(isImageFile(f.getPath())){
imagePath.add(f.getPath());//将图片路径加入到list集合中
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

class MyAdapter extends BaseAdapter{


@Override
public int getCount() {
// TODO Auto-generated method stub
return imagePath.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return imagePath.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(MainActivity.this);
//设置图像的宽和高
imageView.setAdjustViewBounds(true);
imageView.setMaxWidth(500);
imageView.setMaxHeight(400);
imageView.setPadding(5, 5, 5, 5);
}else{
imageView=(ImageView) convertView;
}
Bitmap bitmap = BitmapFactory.decodeFile(imagePath.get(position));
imageView.setImageBitmap(bitmap);
return imageView;
}

}
}

http://www.niftyadmin.cn/n/3649436.html

相关文章

[SIP]SIP安全框架之认证 幻灯片

这是我编写的第三个针对SIP的幻灯片&#xff0c;讲述SIP认证&#xff0c;可用于Team内讲解并演示SIP协议的讲座。本讲义的版权归郑昀所有。允许拷贝、分发和在“GNU Free Documentation License”下的定制。对于关注SIP应用的你&#xff0c;任何的建议和修正都是欢迎的&#xf…

Windows 10安装Tomcat——Web 应用服务器

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器&#xff0c;属于轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多的场合下被普遍使用&#xff0c;是开发和调试JSP 程序的首选。对于一个初学者来说&#xff0c;可以这样认为&#xff0c;当在一台机器上配…

Android dumpsys 命令解析

Android 有一个很有意思&#xff0c;也很有用的命令&#xff1a;dumpsys&#xff0c;它可以 取出一些系统信息。虽然它只是在命令行里显示&#xff0c;但是有一些技术点应该被采用、充实。如果你想得到所有的Android系统信息&#xff0c;包括第三方应用的信息&#xff0c;那么如…

freebsd 9.1.1_如何在FreeBSD 12.0上通过加密来保护Apache

freebsd 9.1.1介绍 (Introduction) Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software cl…

引导界面框架

效果图&#xff1a; 首先自定义一个小红点; <?xml version"1.0" encoding"utf-8"?> <shape xmlns:android"http://schemas.android.com/apk/res/android" android:shape"oval"> <size android:height"10dp…

prisma orm_如何使用Node.js和Prisma构建GraphQL服务器

prisma orm介绍 (Introduction) Prisma is a data layer that turns a database into a GraphQL API. We can look at it as a kind of Object-Relational Mapper (ORM), but it’s much more powerful than traditional ORMs. With Prisma, we get a server (Prisma server) t…

CentOS 7搭建Java开发平台——Java 8

Java是一门面向对象编程语言&#xff0c;不仅吸收了C语言的各种优点&#xff0c;还摒弃了C里难以理解的多继承、指针等概念&#xff0c;因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表&#xff0c;极好地实现了面向对象理论&#xff0c;…

css3字体弹跳动画_如何使用CSS3动画创建弹跳页面加载器

css3字体弹跳动画介绍 (Introduction) In this tutorial, you will create a bouncing page loader using CSS3 animation keyframes. It will show you how to style HTML for a loading page, create animation keyframes, and use animation delay with keyframes. 在本教程…