在我的 Android 应用程序中,我将外部存储的所有路径填充到一个数组中。
少数设备正在报告StackOverflowError。
我已经阅读了许多有关此问题原因的链接帖子,但我不知道如何处理它或防止它在我正在使用的代码中发生。我也不了解 Android 可以处理的“递归限制”。
下面的代码改编自这个来源。
private final Locale loc = SupportedLanguages.isSupported();
private final String CACHE = "cache";
private final String TEMP = "temp";
@Override
protected Boolean doInBackground(Void... params) {
final File fileList = Environment.getExternalStorageDirectory();
final String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
final File[] dirList = fileList.listFiles();
final List<File> listDirs = Arrays.asList(dirList);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
final ArrayList<String> dirPath = new ArrayList<String>();
final ArrayList<String> dirName = new ArrayList<String>();
String fileName = "";
for (final File startingDirectory : listDirs) {
if (!startingDirectory.isFile() && startingDirectory.canRead() && !startingDirectory.isHidden()) {
final List<File> files = getFileListing(startingDirectory);
if (files != null) {
for (final File file : files) {
fileName = file.getPath().replaceAll(absolutePath, "").toLowerCase(loc).replaceAll("\\/", " ")
.trim();
fileName = fileName.replaceAll(" +", " ");
dirName.add(fileName);
dirPath.add(file.toString());
}
}
}
}
}
return true;
}
private List<File> getFileListing(File aStartingDir) {
List<File> result = getFileListingNoSort(aStartingDir);
if (result != null && !result.isEmpty()) {
Collections.sort(result);
}
return result;
}
private List<File> getFileListingNoSort(File aStartingDir) {
List<File> resultArray = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
if (filesAndDirs != null && filesAndDirs.length > 0) {
List<File> filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
if (!file.isFile() && file.canRead() && !file.isHidden() && !file.getName().toLowerCase(loc).startsWith(CACHE)
&& !file.getName().toLowerCase(loc).startsWith(TEMP)) {
resultArray.add(file);
List<File> deeperList = getFileListingNoSort(file);
resultArray.addAll(deeperList);
}
}
}
return resultArray;
}
崩溃日志:
> Caused by: java.lang.StackOverflowError at
> java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
> at java.lang.StringBuilder.append(StringBuilder.java:216) at
> java.io.File.join(File.java:215) at java.io.File.<init>(File.java:157)
> at java.io.File.<init>(File.java:124) at
> java.io.File.filenamesToFiles(File.java:852) at
> java.io.File.listFiles(File.java:791) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source) at
> com.mypackage.name.ll.a(Unknown Source)
等等......
proguard映射:
com.mypackage.name.GenerateSubDirectoryList -> com.mypackage.name.ll:
java.util.List getFileListingNoSort(java.io.File) -> a
在某个地方我需要计算递归并应用限制。但我不知道适用于 Android 或单个设备硬件的位置或限制?
在此先感谢您的帮助。