如何在SD卡上自动创建目录
我正在尝试将文件保存到以下位置FileOutputStream fos = new FileOutputStream("/sdcard/Wallpaper/"+fileName);
但我收到异常 java.io.FileNotFoundException
但是,当我将路径设置为 "/sdcard/"
时,它就可以工作了。
现在我假设我无法以这种方式自动创建目录。
有人可以建议如何使用代码创建目录和子目录
吗?
I'm trying to save my file to the following locationFileOutputStream fos = new FileOutputStream("/sdcard/Wallpaper/"+fileName);
but I'm getting the exception java.io.FileNotFoundException
However, when I put the path as "/sdcard/"
it works.
Now I'm assuming that I'm not able to create directory automatically this way.
Can someone suggest how to create a directory and sub-directory
using code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您创建一个包含顶级目录的 File 对象,您可以调用它的 mkdirs() 方法来构建所有所需的目录。类似于:
注意: 使用 Environment.getExternalStorageDirectory() 用于获取“SD 卡”目录,因为如果手机配备 SD 卡以外的其他设备(例如内置闪存、a'la iPhone)。无论哪种方式,您都应该记住,您需要检查以确保它确实存在,因为 SD 卡可能会被移除。
更新:自 API 级别 4 (1.6) 起,您还必须请求权限。像这样的东西(在清单中)应该有效:
If you create a File object that wraps the top-level directory you can call it's mkdirs() method to build all the needed directories. Something like:
Note: It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change if a phone comes along which has something other than an SD Card (such as built-in flash, a'la the iPhone). Either way you should keep in mind that you need to check to make sure it's actually there as the SD Card may be removed.
UPDATE: Since API Level 4 (1.6) you'll also have to request the permission. Something like this (in the manifest) should work:
有同样的问题,只是想添加 AndroidManifest.xml 也需要此权限:
Had the same problem and just want to add that AndroidManifest.xml also needs this permission:
这对我有用。
在你的清单和下面的代码中
Here is what works for me.
in your manifest and the code below
实际上我使用了 @fiXedd asnwer 的一部分,它对我有用:
确保您使用 mkdirs() 而不是 mkdir() 来创建完整路径
Actually I used part of @fiXedd asnwer and it worked for me:
Make sure that you are using mkdirs() not mkdir() to create the complete path
在 API 8 及更高版本中,SD 卡的位置发生了变化。 @fiXedd 的答案很好,但为了更安全的代码,您应该使用 Environment.getExternalStorageState() 来检查媒体是否可用。然后,您可以使用
getExternalFilesDir()
导航到所需的目录(假设您使用的是 API 8 或更高版本)。您可以在SDK 文档中阅读更多信息。
With API 8 and greater, the location of the SD card has changed. @fiXedd's answer is good, but for safer code, you should use
Environment.getExternalStorageState()
to check if the media is available. Then you can usegetExternalFilesDir()
to navigate to the directory you want (assuming you're using API 8 or greater).You can read more in the SDK documentation.
确保存在外部存储:
http://developer.android.com/guide/topics/data/data-storage。 html#filesExternal
Make sure external storage is present:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
不要忘记确保文件/文件夹名称中没有特殊字符。当我使用变量设置文件夹名称时,出现“:”,
文件/文件夹名称中不允许有字符
在这种情况下,您可能会发现此代码很有帮助。
下面的代码删除所有“:”并用“-”替换它们
Don't forget to make sure that you have no special characters in your file/folder names. Happened to me with ":" when I was setting folder names using variable(s)
not allowed characters in file/folder names
U may find this code helpful in such a case.
The below code removes all ":" and replaces them with "-"
我遇到了同样的问题,无法在 Galaxy S 上创建目录,但能够在 Nexus 和 Samsung Droid 上成功创建目录。我如何修复它是通过添加以下代码行:
I was facing the same problem, unable to create directory on Galaxy S but was able to create it successfully on Nexus and Samsung Droid. How I fixed it was by adding following line of code:
这将在您的 SD 卡中创建一个名为 dor 的文件夹。
然后获取例如 filename.json 的文件,该文件手动插入到 dor 文件夹中。喜欢:
<使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并且不要忘记在清单中添加代码
this will create a folder named dor in your sdcard.
then to fetch file for eg- filename.json which is manually inserted in dor folder. Like:
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and don't forget to add code in manifest
刚刚完成 Vijay 的帖子...
清单
函数
用法
您可以检查错误
Just completing the Vijay's post...
Manifest
Function
Usage
You could check for errors