Android的MKDirs()不是为我工作 - 不是外部存储 [英] Android MKDirs() not working for me - NOT EXTERNAL Storage

查看:252
本文介绍了Android的MKDirs()不是为我工作 - 不是外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个其他各种帖子,还没有发现正在为我一个答案。他们一直在讨论使用外部存储的,我需要使用默认(内部)的存储空间。

I have read over the various other postings on this and have not yet found an answer that is working for me. They have been discussing the use of External Storage and I need to use 'default' (internal) storage.

我有一个非常简单的程序在我的活动例程之一

I have a very simple routine in one of my Activity routines

String PATH = "/data/data/com.mydomain.myapplicationname/files";
SystemIOFile.MkDir(PATH);  // Ensure Recipient Path Exists

然后在我SystemIOFile类我有

And then in my SystemIOFile class I have

static public Boolean MkDir(String directoryName) 
  {
    Boolean isDirectoryFound = false;
    File Dir = new File(directoryName);
    if(Dir.isDirectory())
    {
      isDirectoryFound = true;
    } else {
      Dir.mkdirs();
      if (Dir.isDirectory()) 
      {
         isDirectoryFound = true;
      }
  }
  Dir = null;
  return isDirectoryFound;
}

在我Android.Manifest.xml我有

And in my Android.Manifest.xml I have

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

所以,好像所有的作品都在地方这个工作。

So it seems as though all of the pieces are in place for this to work.

但它不能正常工作。结果
当我单通过它总是失败的MKDIR()函数的一步。结果

BUT it is not working.
When I single step through the MKDir() routine it ALWAYS fails.
The

if (Dir.isDirectory())

总是返回结果
和随后的 Dir.mkdirs()总是返回

我在想什么?

推荐答案

这是一个很大的问题。
首先,它的更好,如果你不直接引用 / SD卡路径。使用 Environment.getExternalStorageDirectory()的getPath()来代替。
其次,假设你要访问自己的应用程序中的文件 - 你也不想引用 /数据/数据​​路径直接,因为它可以有所不同的多用户场景。使用 Context.getFilesDir()的getPath()代替。

This is a great question. First, it's better if you don't reference the /sdcard path directly. Use Environment.getExternalStorageDirectory().getPath() instead. Second, assuming you want to access your own app's files - you also don't want to reference the /data/data path directly because it can vary in multi-user scenarios. Use Context.getFilesDir().getPath() instead.

实施以上,我们看到:

String PATH = "/data/data/com.mydomain.myapplicationname/files";
SystemIOFile.MkDir(PATH);

返回,而

String PATH = Environment.getExternalStorageDirectory().getPath() 
        + getApplicationContext().getFilesDir().getPath();
SystemIOFile.MkDir(PATH);

返回

另外请注意,你忽略了的结果Dir.mkdirs()布尔的mkdir(字符串目录名)

Also note that you ignored the result of Dir.mkdirs() inside Boolean MkDir(String directoryName) .

希望这有助于。

这篇关于Android的MKDirs()不是为我工作 - 不是外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆