首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android mkdirs()返回false

Android mkdirs()返回false
EN

Stack Overflow用户
提问于 2011-08-01 23:13:27
回答 1查看 3.6K关注 0票数 1

我有一个Android ()返回false。我读到了另一个你对类似问题的回答。你提到了一些关于Environment.getExternal ..。无论如何,我在Droid上运行Android2.2。sd卡已经挂载,并且两个返回值都是可读的和可写的返回true。这是我的密码。

代码语言:javascript
复制
public void writeToExternalStoragePublic(String filename, byte[] content) {

    // API Level 7 or lower, use getExternalStorageDirectory() 
    //  to open a File that represents the root of the external 
    // storage, but writing to root is not recommended, and instead 
    // application should write to application-specific directory, as shown below.

    String packageName = this.getPackageName();
    Toast.makeText(this, packageName, 1000).show();
    String path = "/Android/data/";// + packageName + "/files/";

    if (isExternalStorageAvailable() && 
       !isExternalStorageReadOnly()) {
        try {
            File file = new File(path, filename);
            file.mkdirs();
            file.mkd
            Toast.makeText(this, "Made Dirs = " + file.mkdirs(), 1000).show();
            Toast.makeText(this, "Made Dir = " + file.mkdir(), 1000).show();

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(content);
            fos.close();
            Toast.makeText(this, "success", 1000).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "fnf", 1000).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "io", 1000).show();
        } catch (SecurityException e) {
            Toast.makeText(this, "security", 1000).show();
        }
    }
}

谢谢。如果你知道有什么办法,我会很感激的。谢谢。

这段代码实际上是来自其他人的一个例子,我正在努力使它发挥作用。

http://www.ibm.com/developerworks/xml/library/x-androidstorage/index.html

德兰

EN

回答 1

Stack Overflow用户

发布于 2011-08-01 23:33:28

File.mkdirs()只返回一次true :当目录实际上必须创建时。从文件中:

如果创建了必要的目录,

将返回true;如果目标目录已经存在或无法创建其中一个目录,则返回false。

因此,除非删除该目录,否则它将每隔一次返回false()。如果您想知道目录是否存在,请使用File.isDirectory()

FWIW,我没有DroidX,所以我不能与您正在使用的路径说话,但是这个路径在我的设备上是行不通的。我还建议检查路径(使用亚行shell),以确保您要创建的路径是有效的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6905550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档