mkdirs 似乎不适用于 Activity 的 onCreate 中的 sdcard
我正在使用一种方法在活动的 onCreate 中使用 mkdirs 创建目录结构。尽管它返回 true 并且所有指示都表明它应该工作,但目录仍然没有在 SD 卡上创建(或者可能不可见)。可能是什么问题?
更新: 1.我已经给了android.permission.WRITE_EXTERNAL_STORAGE 2. 如果从 onCreate 以外的其他地方调用该方法,即创建目录结构,则该方法可以正常工作。
public static void createNoMediaFile() {
Log.v("myreader",">>>>>>>>>>>>>>> Entered createNoMediaFile");
File papermag=new File(DigitalEditionConstant.PAPERMAG_PATH);
boolean isdircreated=papermag.mkdirs();
Log.v("myreader",">>>>>>>>>>>>>>>>Directory setup: "+isdircreated);
File noMediaFile=new File(DigitalEditionConstant.DIR_PATH+".nomedia");
if(!noMediaFile.exists()){
try {
noMediaFile.createNewFile();
Log.v("myreader",">>>>>>>>>>>>>>>>>>> Created File: "+noMediaFile.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
目录路径等全部检查完毕。该方法是从我的项目中活动的 onCreate 调用的
I am using a method to create a directory structure using mkdirs in onCreate of an activity. Eventhough it returns true and all indiactions are that it should work, still the directories are not created (or perhaps not visible) on the sdcard. What might be the problem?
Update:
1. I have given the android.permission.WRITE_EXTERNAL_STORAGE
2. The method works fine if called from anywhere else other than onCreate, i.e the directory structure is created.
public static void createNoMediaFile() {
Log.v("myreader",">>>>>>>>>>>>>>> Entered createNoMediaFile");
File papermag=new File(DigitalEditionConstant.PAPERMAG_PATH);
boolean isdircreated=papermag.mkdirs();
Log.v("myreader",">>>>>>>>>>>>>>>>Directory setup: "+isdircreated);
File noMediaFile=new File(DigitalEditionConstant.DIR_PATH+".nomedia");
if(!noMediaFile.exists()){
try {
noMediaFile.createNewFile();
Log.v("myreader",">>>>>>>>>>>>>>>>>>> Created File: "+noMediaFile.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Directory paths etc. are all checked and ok. This method is called from onCreate of an activity in my project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否在清单文件中添加了 WRITE_EXTERNAL_STORAGE 权限?请参阅此处
have you added permission of WRITE_EXTERNAL_STORAGE in your manifest file? see here
这是简单的代码,
在您的菜单中添加此权限...
here is simple code
add this permission in your menifest...
目录的名称是否以句点开头? DDMS 和大多数文件管理器应用程序不会显示这些目录......但它们就在那里。
您是否添加了清单权限:
将设备安装为 USB 也会阻止从代码访问 SD。
does the name of the dir start with a period? DDMS and most file manager apps wont show those dirs..but they are there.
Have you added the manifest permission:
Having the device mounted as USB will block access to the SD from code also.
使用“adb push”命令检查是否能够将目录复制到SD卡上(确定SD卡没有问题)
Use "adb push" command to check if you are able to copy the directory to the SD card (to determine that there is no issue with the SD card)