我一直试图让这个工作,但我在某处磕磕绊绊。
这是整个事情的主要来源。而logcat结果......有些是多余的。我在onCreate上面有“startingDirectory”,所以我复制并删除了私有...
String startingDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
String directory = startingDirectory;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) == false) {
Log.d(TAG, "Media is not mounted.");
} else {
Log.d(TAG, "Loading File Directory.");
Log.d(TAG, "Directory listing for " + directory);
File path = new File(directory);
//File path = new File(directory);
if (path.isDirectory()) {
if (path.canRead()) {
Log.d(TAG, "Readable directory");
} else {
Log.d(TAG, "Non-readable directory");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.d(TAG, "Media is mounted");
} else {
Log.d(TAG, "Media is not mounted");
}
}
} else {
Log.d(TAG, "Is not a directory.");
}
}
这是整个事情的logcat ......
07-13 01:13:45.705 4361-4361/? D/MainActivity: Loading File Directory.
07-13 01:13:45.705 4361-4361/? D/MainActivity: Directory listing for /storage/emulated/0
07-13 01:13:45.705 4361-4361/? D/MainActivity: Non-readable directory
07-13 01:13:45.706 4361-4361/? D/MainActivity: Media is mounted
我正在尝试获取当前目录(staringDirectory)的目录结构并放入一个类......但它似乎不能保持不可读
答案 0 :(得分:0)
这是我的permitFileAcess方法,每当我修改一些文件和目录的权限时,使用java.io.File类的setWritable和setReadable方法。 好吧,我仍然有消息,但它对我有用
public static void permitFileAcess(final File file) {
if (!file.setWritable(true, true)) {
log.e(new Object[] { "Could not set Writable permissions" });
}
if (!file.setReadable(true, true)) {
log.e(new Object[] { "Could not set Readable permissions" });
}
if (!file.setExecutable(true, true)) {
log.e(new Object[] { "Could not set Executable permissions" });
}
}
因此,如果要创建文件,请在操作之前使用它。
private static boolean createFile(File parent, String fileName, boolean createDir) {
boolean returnValue = false;
if (parent.isDirectory()) {
File newFile = new File(parent.getAbsolutePath(), fileName);
permitFileAcess(newFile);
if (createDir) {
returnValue = newFile.mkdir();
} else {
try {
returnValue = newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return returnValue;
}
祝你好运。
答案 1 :(得分:0)
我试图检索特定目录中的所有文件和目录。制作一个简单的文件管理器。如果我没有看到该文件,我将无法知道要加载哪个文件。