java 如何在Android中创建目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4776426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:58:18  来源:igfitidea点击:

How to create a directory in Android?

javaandroidandroid-sdcard

提问by Dimitri

Everything is in the question. Here is my code :

一切都在问题中。这是我的代码:

    private void createDirectory(File currentDirectory) {
  File f = null;
  try {

   f = new File(currentDirectory.getCanonicalPath() + "/directory"
     + count);

   boolean success = f.mkdir();
   if (!success) {
    Toast.makeText(currentContext,
      f.getName() + " could not be created", 15).show();

   }
  } catch (IOException ioe) {
   Toast.makeText(currentContext,
     f.getName() + " could not be created", 15).show();
  }

  count++;
 }

I am writing a small file manager in Android and I would like to add the possibility to create a directory. There is no exception and the variable success always return false. Can someone tell me what is wrong my code??

我正在 Android 中编写一个小文件管理器,我想添加创建目录的可能性。没有例外,变量成功总是返回假。有人能告诉我我的代码有什么问题吗??

Thx for your advice !!

谢谢你的建议!!

[EDIT]

[编辑]

BTW, When the phone is in Developpement mode, does an application has a write access on the sdcard? I am programming on my phone (Acer liquid)

顺便说一句,当手机处于开发模式时,应用程序是否具有对 SD 卡的写访问权限?我正在我的手机上编程(宏基液体)

回答by Cristian

You have to add this permission:

您必须添加此权限:

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

By the way, not sure how you are getting the SDcard directory... but it must be this way:

顺便说一句,不确定您是如何获取 SDcard 目录的……但它必须是这样的:

File sdDir = Environment.getExternalStorageDirectory();

It's important, because some programmers use to think that the SDCard is always /sdcard, but it could change.

这很重要,因为有些程序员过去认为 SDCard 总是 /sdcard,但它可能会改变。

回答by AaronM

You're going to need to add

你需要添加

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

to your manifest file to tell Android to ask the user for your application to be allowed to.

到您的清单文件,告诉 Android 要求用户允许您的应用程序。

回答by CommonsWare

First, please use the Fileconstructor that takes a Filefor your directory and a Stringfor your desired subdirectory.

首先,请使用为您的目录使用FileaFileString为您想要的子目录使用 a 的构造函数。

Beyond that, most likely you are attempting to create a directory where you are not allowed, either because:

除此之外,很可能您正在尝试创建一个不允许的目录,原因可能是:

  • currentDirectorydoes not exist, or
  • currentDirectorypoints to a place on the on-board flash storage where you are not allowed to write, or
  • currentDirectorypoints to a place on the external storage and you lack the WRITE_EXTERNAL_STORAGEpermission
  • currentDirectory不存在,或
  • currentDirectory指向板载闪存上不允许写入的位置,或
  • currentDirectory指向外部存储上的一个位置,但您没有WRITE_EXTERNAL_STORAGE权限

回答by tony gil

instead of boolean success = f.mkdir();use

而不是boolean success = f.mkdir();使用

boolean success = f.mkdirs();

that solved my problem (i used the same reference code as you and had the same issue).

这解决了我的问题(我使用了与您相同的参考代码并且遇到了同样的问题)。