我需要计算存储在SD卡中的图像数量,所以我想命名像Sample0,Sample1,Sample2,Sample3等独特的图像。 可能吗?如何获取存储在SD卡中的图像的数量
2
A
回答
9
它提供的图像出现在你的SD卡的图片文件夹数量:
File dir = new File(Environment.getExternalStorageDirectory()
+ "/images");
File[] files = dir.listFiles();
int numberOfImages=files.length;
1
我现在它已经很长一段时间,但是,上面的方法会给你任何类型的文件的数量和images文件夹中的目录,不仅是图像编号,也不会在images目录内的子文件夹中查找图像。
比方说,我们有这样的结构:
Images/
- img1.jpg
- img2.jpg
- data.dat
- whatever.pdf
- Folder/
- img3.png
上述方法,会给你5,你应该获得3是不是?
我一直在做类似的事情,我敢肯定,有一种更有效的方法,但通过这种方法,我得到了SDCard或任何给定目录及其子文件夹中所有图像的数量......
这里,我们去:
public int countImgs(File file, int number) {
File[] dirs = file.listFiles();
String name = "";
if (dirs != null) { // Sanity check
for (File dir : dirs) {
if (dir.isFile()) { // Check file or directory
name = dir.getName().toLowerCase();
// Add or delete extensions as needed
if (name.endsWith(".png") || name.endsWith(".jpg")
|| name.endsWith(".jpeg")) {
number++;
}
} else number = countImgs(dir, number);
}
}
return number;
}
凡帕拉姆文件是根目录和数量将在0开始。因此,要使用它:
int imgNumber = countImgs(Environment.getExternalStorageDirectory(), 0);
1
这将是当文件= null当获取存储在SD卡中的图像的数量
File dir = new File(Environment.getExternalStorageDirectory() + "/images");
File[] files = dir.listFiles();
String strnumberOfImages=files.length;
int intnumberOfImages= 0;
try{
intnumberOfImages = Integer.parseInt(strnumberOfImages);
}catch (Exception e){
intnumberOfImages = 0;
}
如果只检查空的错误或不
File dir = new File(Environment.getExternalStorageDirectory()+ "/images");
File[] files = dir.listFiles();
if(files != null){
// there are picz
}else{
// sd card empty
}
cheerz!
相关问题
- 1. 如何获取存储在SD卡上的图像的Uri?
- 2. 如何从存储在SD卡上的图像获取图像路径
- 3. 如何在SD卡中的文件中存储位图图像?
- 4. 如何:在SD卡中存储图像,存储和获取配置文件的共享首选图像路径
- 5. 崩溃时,获取存储在SD中的图像数
- 6. 如何删除存储在sd卡上的位图图像android
- 7. 从SD卡中获取图像并将其存储在Android数据库中
- 8. 获取存储在SD卡+安卓
- 9. 如何发送图像而不存储在SD卡中?
- 10. 如何随时在SD卡中存储图像
- 11. 如何从android中的SD卡获取指定的图像?
- 12. 从SD卡中获取图像 - android
- 13. 如何将android中的SD卡中的图像存储到数据库中?
- 14. 存储在SD卡中的Android数据
- 15. 将图像存储到SD卡?
- 16. 获取存储在SD卡中的视频的路径
- 17. 如何从android中的SD卡获取图像?
- 18. 捕获图片存储到SD卡
- 19. 如何将下载的图像存储到Android SD卡
- 20. 如何获取存储在Android手机的SD卡上的图像的元数据
- 21. 如何存储和获取图像变量(SESSION)的图像?
- 22. 如何访问存储在我的SD卡中的图像文件
- 23. 从Gopro SD卡获取图像Objective-c
- 24. 在具有内置存储和外部SD卡的电话中存储图像
- 25. 如何捕获图像,并将其保存在SD卡
- 26. 如何从SD卡获取歌曲并将歌曲存储在数据库中
- 27. 从SD卡中取出图像,压缩并保存回SD?
- 28. 要将图像名称存储在SD卡的Android模拟器
- 29. 无法读取存储在SD卡
- 30. 如何从驱动器或SD卡中获取图像并存储在阵列中
伟大的工作人..任何时候感谢 – 2012-02-23 07:06:47
..你可以使用filterfiles来限制文件类型 – Chet 2012-02-23 07:18:11
不能理解。为什么我们需要限制文件类型 – 2012-02-23 08:26:31