我需要计算存储在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卡中的图像数量
- 2. 如何获取存储在SD卡上的图像的Uri?
- 3. 如何从存储在SD卡上的图像获取图像路径
- 4. 如何在存储SD卡之前裁剪捕获的图像?
- 5. 如何在SD卡中的文件中存储位图图像?
- 6. 如何:在SD卡中存储图像,存储和获取配置文件的共享首选图像路径
- 7. Android如何旋转存储在SD卡中的图像文件?
- 8. 捕获的图像无法存储在SD卡中?
- 9. 崩溃时,获取存储在SD中的图像数
- 10. 如何删除存储在sd卡上的位图图像android
- 11. 从SD卡中获取图像并将其存储在Android数据库中
- 12. 获取存储在SD卡+安卓
- 13. 如何下载图像并存储在Android的SD卡?
- 14. 如何显示存储在SD卡上的图像?
- 15. 如何随时在SD卡中存储图像
- 16. 如何发送图像而不存储在SD卡中?
- 17. 获取内部存储和SD卡的目录(如果存在)
- 18. 在GridView中显示存储在SD卡中的图像
- 19. 如何从android中的SD卡获取指定的图像?
- 20. 如何将android中的SD卡中的图像存储到数据库中?
- 21. 存储在SD卡中的Android数据
- 22. 从SD卡中获取图像 - android
- 23. 将图像存储到SD卡?
- 24. 保存图像视图中的SD卡
- 25. 如何在SD卡中保存捕获图像?
- 26. 获取存储在SD卡中的视频的路径
- 27. 如何从Android SD卡和内部存储器获取图像和视频
- 28. 在SD卡中存储在sdcard中的数据库点击后保存图像
- 29. android如何获取sd卡中的特定图像
- 30. 如何从android中的SD卡获取图像?
伟大的工作人..任何时候感谢 – 2012-02-23 07:06:47
..你可以使用filterfiles来限制文件类型 – Chet 2012-02-23 07:18:11
不能理解。为什么我们需要限制文件类型 – 2012-02-23 08:26:31