可以获取Android设备的内部下载文件夹路径吗?
答案 0 :(得分:32)
如果设备有SD卡,请使用:
Environment.getExternalStorageState()
如果您没有SD卡,请使用:
Environment.getDataDirectory()
如果没有SD卡,您可以在本地设备上创建自己的目录。
//if there is no SD card, create new directory objects to make directory on device
if (Environment.getExternalStorageState() == null) {
//create new file directory object
directory = new File(Environment.getDataDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(Environment.getDataDirectory()
+ "/Robotium-Screenshots/");
/*
* this checks to see if there are any previous test photo files
* if there are any photos, they are deleted for the sake of
* memory
*/
if (photoDirectory.exists()) {
File[] dirFiles = photoDirectory.listFiles();
if (dirFiles.length != 0) {
for (int ii = 0; ii <= dirFiles.length; ii++) {
dirFiles[ii].delete();
}
}
}
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
// if phone DOES have sd card
} else if (Environment.getExternalStorageState() != null) {
// search for directory on SD card
directory = new File(Environment.getExternalStorageDirectory()
+ "/RobotiumTestLog/");
photoDirectory = new File(
Environment.getExternalStorageDirectory()
+ "/Robotium-Screenshots/");
if (photoDirectory.exists()) {
File[] dirFiles = photoDirectory.listFiles();
if (dirFiles.length > 0) {
for (int ii = 0; ii < dirFiles.length; ii++) {
dirFiles[ii].delete();
}
dirFiles = null;
}
}
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}// end of SD card checking
在manifest.xml上添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
快乐编码..
答案 1 :(得分:12)
要查找应用内部存储空间的位置,请使用 getFilesDir(),在任何上下文(例如您的活动)上调用,以获取File对象。
为什么不能使用 Environment.getExternalStorageDirectory()从代码中获取/ storage / sdcard0 / path?
也需要添加权限。
<强> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
强>
希望你能得到你的解决方案......