Android mkdir()返回false,不创建文件夹 [英] Android mkdir() returns false, doesn't create folder

查看:534
本文介绍了Android mkdir()返回false,不创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯。我已经尝试过各种语法,但是 mkdirs() mkdirs()都返回false。

This is driving me crazy. I have tried all kinds of syntaxes but both mkdir() and mkdirs() return false.

我的代码:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

File folder = new File(extStorageDirectory, "myportal");
boolean bool = folder.mkdir();

File pdfFile = new File(folder, fileName);

try{
    pdfFile.createNewFile();
}catch (IOException e){
    e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);

我正在获取 IOException:没有这样的文件或目录尝试创建文件时。 logcat部分显示没有创建目录:

I was getting and IOException: No such file or directory when trying to create the file. The logcat part showed me that no directory was created:

Log.d("BG", "URL: " + fileUrl);
Log.d("BG", "pdfFile: " + pdfFile);
Log.d("BG", "Ext Storage: " + extStorageDirectory);
Log.d("BG", "Ext storage state: " + Environment.getExternalStorageState().toString());
Log.d("BG", "Mkdir return: " + bool);
Log.d("BG", "IsDirectory: " + folder.isDirectory());

打印内容:

05-26 22:43:03.797 19364-30646/com.kristmiha.myportal2 D/BG: URL: http://192.168.100.65:80/myportal/upload/orari.pdf
05-26 22:43:03.798 19364-30646/com.kristmiha.myportal2 D/BG: pdfFile: /storage/emulated/0/myportal/orari.pdf
05-26 22:43:03.798 19364-30646/com.kristmiha.myportal2 D/BG: Ext Storage: /storage/emulated/0
05-26 22:43:03.804 19364-30646/com.kristmiha.myportal2 D/BG: Ext storage state: mounted
05-26 22:43:03.805 19364-30646/com.kristmiha.myportal2 D/BG: Mkdir return: false
05-26 22:43:03.805 19364-30646/com.kristmiha.myportal2 D/BG: IsDirectory: false

我已经仔细检查了权限我将它们放在正确的位置。我想我读过某个地方,在 KitKat 之后,我们不允许在外部存储中写入数据,但尚未找到解决方案。

I've double checked permissions and I've put them in the right place. I think I read somewhere that after KitKat we are not allowed to write in the external storage, but have found no solution yet.

推荐答案

创建外部存储目录需要 WRITE_EXTERNAL_STORAGE 许可;没有此许可,尝试写入外部存储将失败。另外,并非所有目录都必须可写;使用 getExternalStoragePublicDirectory()获取具有共享写访问权限的目录(其他外部目录可能是只读的)。但是,如果写入外部存储的目的是与其他应用程序共享文件,则应强烈考虑使用 FileProvider API(另请参见设置文件共享);通过这种策略,您的应用程序会将文件存储在其自己的内部特定于应用程序的目录中,然后通过内容提供程序选择性地将这些文件共享给其他应用程序。这种策略为文件提供了更高的安全性,还使您可以对文件的读取/写入提供更好的访问控制。

Creating external storage directories requires the WRITE_EXTERNAL_STORAGE permission; without this permission, attempts to write external storage will fail. Also, not all directories are necessarily writable; use getExternalStoragePublicDirectory() to get a directory that has shared write access (other external directories may be read-only). However, if the purpose of writing to external storage is to share the file with other applications, you should strongly consider using the FileProvider API, instead (see also Setting Up File Sharing); with this strategy, your app stores files in its own, internal app-specific directories, but then enables selective sharing of these files to other apps through a content provider. This strategy provides greater security for the files and also makes it possible for you to provide greater access control over reading/writing of the files.

因为您说自己已经拥有所需的权限*,最有可能导致您出错的地方是调用 toString()。无法保证文件上的 toString()方法返回其完整路径。串联时使用 getPath() getAbsolutePath()。还建议在选择写入外部存储时,首先检查其状态

Since you say that you already have the required permission*, most likely where you are getting things wrong is in the call to toString(). There is no guarantee that the toString() method on a file returns its full path. Use getPath() or getAbsolutePath() when concatenating these. It is also advisable, when choosing to write to external storage, that you first check its state; the external storage can in some cases be ejected/unmounted and not available.

*您应该使用 ContextCompat.checkSelfPermission()。您应该添加对 requestPermissions()(如果权限不存在)。

*You should verify this with ContextCompat.checkSelfPermission(). You should add a call to requestPermissions() if the permissions are not present.

这篇关于Android mkdir()返回false,不创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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