mkdirs 似乎不适用于 Activity 的 onCreate 中的 sdcard

发布于 2024-12-01 05:24:22 字数 1113 浏览 2 评论 0原文

我正在使用一种方法在活动的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪冰清 2024-12-08 05:24:22

您是否在清单文件中添加了 WRITE_EXTERNAL_STORAGE 权限?请参阅此处

have you added permission of WRITE_EXTERNAL_STORAGE in your manifest file? see here

赠我空喜 2024-12-08 05:24:22

这是简单的代码,

File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/FolderName");
wallpaperDirectory.mkdirs();

在您的菜单中添加此权限...

WRITE_EXTERNAL_STORAGE 权限

here is simple code

File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/FolderName");
wallpaperDirectory.mkdirs();

add this permission in your menifest...

permission of WRITE_EXTERNAL_STORAGE

许你一世情深 2024-12-08 05:24:22

目录的名称是否以句点开头? DDMS 和大多数文件管理器应用程序不会显示这些目录......但它们就在那里。

您是否添加了清单权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

将设备安装为 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:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Having the device mounted as USB will block access to the SD from code also.

狼性发作 2024-12-08 05:24:22

使用“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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文