无法在Android上的外部存储创建文件夹 [英] Can't create folder on external storage on android

查看:270
本文介绍了无法在Android上的外部存储创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的手机是的Nexus 5 ,运行安卓4.4.2

My development phone is a Nexus 5, running Android 4.4.2.

在我的应用程序,我试图创建外部存储将存储调试信息我的应用程序的文件夹。基本上,它包含所有由应用程序执行的命令,以使当用户遇到的问题,我必须具有它们发送我从调试夹到的信息进行分析的选项。

In my application, I am attempting to create a folder on external storage that will store debug information for my application. Basically it will contain all the commands executed by the application, so that when a user encounters a problem, I have the option of having them send me the information from the debug folder to analyse.

我开始试图写一个文件夹,却发现有创建该文件夹的错误。起初我是用的mkdir(),然后让我感动到 mkdirs()也没有工作。

I started off by trying to write a file to the folder, but found there was an error creating the folder. At first I was using mkdir(), then I moved onto mkdirs() which also didn't work.

我有<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/> 在我的清单。

下面是$ C $下创建的文件夹:

Here is the code for creating the folder:

    File folder = new File(Environment.getExternalStorageDirectory() + "/DebugData");

    String path = folder.getPath();

    if(!folder.mkdirs() || !folder.exists()){        
            Log.e(LOG_TAG, path + " failed");
        } else {
            Log.d(LOG_TAG, path + " succeeded");
        } 

下面是我自己也尝试:

    //Check SD card state
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || !Environment.MEDIA_MOUNTED.equals(state)) {
        Log.e(LOG_TAG, "Error: external storage is read only or unavailable");
    } else {
        Log.d(LOG_TAG, "External storage is not read only or unavailable");
    }

这将返回外部存储的不可以只读或不可用。

This returns that the external storage is not read only or unavailable.

我也尝试过不同的路径,如文件夹=新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),Folder1中);

I have also tried different paths, such as File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Folder1");

这是它成为真正的混乱。

我尝试开发不同的手机。首先,我抓住了一个银河S4 GT-i9505 运行安卓4.2.2 和它的工作。我能够创建文件夹,并写信给他们。这说明我的code的工作。同时通过在S4和Nexus 5运行code返回的路径是一样的。

I tried development on different phones. Firstly, I grabbed a Galaxy S4 GT-i9505 running Android 4.2.2 and it worked. I was able to create the folders and write to them. This showed me that the code was working. Also the path returned by running the code on the S4 and Nexus 5 was the same.

然后我想这可能是android版特有的。所以我抓住一个的Nexus 4 安卓4.4.2 和code上工作过。创建的文件夹,让我给他们写信。

Then I thought it may be android version specific. So I grabbed a Nexus 4 with Android 4.4.2 and the code worked on it as well. Created the folders and allowed me to write to them.

无的手机扎根,并全部股票的标准。有没有特殊的应用程序或任何我能想到的设置明智的的Nexus 5会导致权限问题。连接设置为媒体设备(MTP)。

None of the phones are rooted and are all stock standard. There's no special applications or anything I can think of settings wise on the Nexus 5 that would cause permissions problems. The connection is set to Media Device (MTP).

编辑:

我要补充一点,我已经试过这也没有工作如下:

I should add that I have tried the follow which also did not work:

  • 在写一个文件到外部存储设备的根目录
  • 创建的文件在外部存储根目录写入它
  • 在创建中列出的路径的文件夹和文件写入到它
  • 在创建中列出的路径中的文件,并写入到它

我很困惑,是什么原因造成这一点,有什么事我可以检测或更改来解决这个问题?

I am confused as to what is causing this, is there anything else I can test or change to fix the issue?

感谢。

推荐答案

在此是一个索引问题与Nexus而言,这个工作对我来说:

In terms of this being an indexing issue with the Nexus, this worked for me:

MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
});

您应该创建并保存文件后直接调用它。通过使用扫描仪,我能看到新创建的文件和目录,只需重新插入设备

You should call it straight after creating and saving the file. By using the scanner, I was able to see newly created files and directories simply by replugging the device in.

按照文档

MediaScannerConnection提供了一种方法为应用程序传递一个新   创建或下载媒体文件的媒体扫描服务。该   媒体扫描服务将读取文件的元数据,并添加   文件到媒体内容提供商

MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.

希望这可以帮助别人。

这篇关于无法在Android上的外部存储创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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