Android获取SD卡根目录下图片

本文提供了一段Java代码示例,用于从指定目录中获取所有图片文件的路径,并检查这些文件是否为常见的图片格式(如JPG, PNG等)。此外,还展示了如何使用这些路径加载图片并将其绘制到画布上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.获取某个目录下图片路径list

[代码]java代码:

01 /**
02 * Get pictures under directory of strPath
03 * @param strPath
04 * @return list
05 */
06 public List getPictures(final String strPath) {
07   List list = new ArrayList();
08    
09   File file = new File(strPath);
10   File[] files = file.listFiles();
11    
12   if (files == null) {
13       return null;
14   }
15    
16   for(int i = 0; i < files.length; i++) {
17       final File f = files[i];
18       if(f.isFile()) {
19           try{
20               int idx = f.getPath().lastIndexOf(".");
21               if (idx <= 0) {
22                   continue;
23               }
24               String suffix = f.getPath().substring(idx);
25               if (suffix.toLowerCase().equals(".jpg") ||
26                   suffix.toLowerCase().equals(".jpeg") ||
27                   suffix.toLowerCase().equals(".bmp") ||
28                   suffix.toLowerCase().equals(".png") ||
29                   suffix.toLowerCase().equals(".gif") )
30               {
31                   list.add(f.getPath());
32               }
33           catch(Exception e) {
34               e.printStackTrace();
35           }
36       }
37   }
38    
39   return list;
40 }
2.调用上述方法获取sd卡下的图片并显示

[代码]java代码:

01 List list = getPictures(Environment.getExternalStorageDirectory() + "");
02 if (list != null) {
03     Log.d(TAG, "list.size = " + list.size());
04     for (int i = 0; i < list.size(); i++) {
05         Bitmap bm = BitmapFactory.decodeFile(list.get(i));
06         int top = 30;
07         if (i > 0) {
08             top += BitmapFactory.decodeFile(list.get(i - 1)).getHeight() + 2;
09         }
10         canvas.drawBitmap(bm, 0, top, paint);
11     }
12 }
13 else {
14     Log.d(TAG, "list is null!!!");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值