【发布时间】:2018-05-21 01:58:39
【问题描述】:
我将一个名为 profiles 的文件夹放在我的 java 文件存储的同一目录中。
我试图找到该文件夹,但得到一个未找到的错误。
String dir = getApplicationInfo().dataDir;
Log.d("dir", dir);
File folder = new File("/profiles"); // also tried File folder = new File(dir+"/profiles");
if (!folder.exists()) {
Log.d("Not Found Dir", "Not Found Dir ");
} else {
Log.d("Found Dir", "Found Dir " );
}
打印
D/dir: /data/user/0/com.pakhocheung.o
D/Not Found Dir: Not Found Dir
然后我尝试列出该目录中的所有文件
String path = dir;
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());
}
打印
D/Files: Path: /data/user/0/com.pakhocheung.o
D/Files: Size: 6
D/Files: FileName:cache
D/Files: FileName:code_cache
D/Files: FileName:lib
D/Files: FileName:shared_prefs
D/Files: FileName:app_Paas
D/Files: FileName:files
我似乎在错误的目录中,因为我看不到这些文件。有什么建议吗?
【问题讨论】:
-
我从未见过这样的用法。尝试将您的文件夹放入 assets 或 raw 中。
-
如果我把文件放在assets里,我怎么能得到那个路径?
-
file:///android_asset/profiles/ -
成功了吗?有什么问题吗?
-
尚未成功。试。我只是尝试
String path = "file:///android_asset/"来查看所有文件。我收到 java.lang.NullPointerException 错误
标签: android file android-studio directory file-exists