安卓文件创建失败[英] Android file-creation fails

本文是小编为大家收集整理的关于安卓文件创建失败的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我使用以下代码在Android单元的SDCARD上创建一个文件夹" mymir"​​和一个文件" .nomedia"(在mymir-folder中).但是,以某种方式失败了,除了将" .nomedia" -file放置在不存在的文件夹中.这是代码:

private String EnsureRootDir() throws IOException
{
    File sdcard = Environment.getExternalStorageDirectory();
    File mymirFolder = new File(sdcard.getAbsolutePath() + "/mymir/");
    if(!mymirFolder.exists())
    {
        File noMedia = new File(mymirFolder.getAbsolutePath() + "/.nomedia");
        noMedia.mkdirs();
        noMedia.createNewFile();
    }
    return mymirFolder.getAbsolutePath();
}

推荐答案

我真的在那里?

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) == true

如果针对1.6+,您是否声明

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

?
确切的例外可能会有所帮助.

本文地址:https://www.itbaoku.cn/post/102498.html

问题描述

I use the following code to create a folder "mymir" and a file ".nomedia" (in the mymir-folder) on the sdcard of an android unit. However, somehow it fails with the exception that the folder the ".nomedia"-file is to be placed in dosn't exist. Here's the code:

private String EnsureRootDir() throws IOException
{
    File sdcard = Environment.getExternalStorageDirectory();
    File mymirFolder = new File(sdcard.getAbsolutePath() + "/mymir/");
    if(!mymirFolder.exists())
    {
        File noMedia = new File(mymirFolder.getAbsolutePath() + "/.nomedia");
        noMedia.mkdirs();
        noMedia.createNewFile();
    }
    return mymirFolder.getAbsolutePath();
}

推荐答案

I SD really there?

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) == true

If targeting 1.6+, have you declared

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

?
The exact Exception could help.