我现在正在android中构建一个文件共享应用程序,我试图获取某个目录上的所有文件(在我的情况下,它是mp3目录)。当我使用函数File.listFiles()
进行它时,它无缘无故地返回null。
directorys = new File(Environment.getExternalStorageDirectory().toString()+"/mp3");
Log.d(TAG,directorys.listFiles().length+"");
之后是否为该listFiles提供了一个异常? 顺便说一下,我已经使用了这个权限
<uses-permission- android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 0 :(得分:0)
试试这段代码
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources);
loadBootstrapProperties(propertySources);
if (isAppConfigLocal()) {
try {
propertySources.addLast(new ResourcePropertySource("appEnvironmentSpecificProperties", getLocalPropertyName()));
propertySources.addLast(new ResourcePropertySource("appCommonProperties", assembalePropertyName(LOCAL)));
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new IllegalStateException("Could not locate local PropertySource(s)", e);
}
} else {
PropertySource<?> source = initializeConfigServicePropertyLocator(this);
if (source != null) {
propertySources.addLast(source);
}
}
}
从marshmallow及以上版本中,您需要添加权限programaticaly
String path = Environment.getExternalStorageDirectory().toString()+"/mp3";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}