简体   繁体   English

无法在外部存储公共目录中创建目录

[英]Unable to create directory in External Storage public directory

I am attempting to create a directory and file so that I can store files that I have downloaded from the Internet. 我正在尝试创建一个目录和文件,以便我可以存储我从Internet下载的文件。 However, I am unable to create the directory and file in the external public storage. 但是,我无法在外部公用存储中创建目录和文件。 If I write to the external private storage (specific storage location for the app) I am perfectly able to do so. 如果我写入外部私有存储(应用程序的特定存储位置),我完全能够这样做。

Here is the code: 这是代码:

    String storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
    System.out.println(storageDirectory);
    File mediaDir = new File(storageDirectory);
    if (!mediaDir.exists()) {
        System.out.println(mediaDir.mkdirs());
        System.out.println(mediaDir.isDirectory());
    }
    File mediaFile = new File(storageDirectory, "boga.jpg");
    try {
        mediaFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

The above code results in: 上面的代码导致:

09-13 05:33:45.258 5867-5867/? 09-13 05:33:45.258 5867-5867 /? I/System.out﹕ /storage/0CED-0F09/Pictures I / System.out:/ storage / 0CED-0F09 /图片

09-13 05:33:45.260 5867-5867/? 09-13 05:33:45.260 5867-5867 /? I/System.out﹕ false I / System.out:false

09-13 05:33:45.260 5867-5867/? 09-13 05:33:45.260 5867-5867 /? I/System.out﹕ false I / System.out:false

With an IOException (No such file or directory): 使用IOException(没有这样的文件或目录):

09-13 05:33:45.260 5867-5867/? 09-13 05:33:45.260 5867-5867 /? W/System.err﹕ java.io.IOException: open failed: ENOENT (No such file or directory) W / System.err:java.io.IOException:open failed:ENOENT(没有这样的文件或目录)

09-13 05:33:45.260 5867-5867/? 09-13 05:33:45.260 5867-5867 /? W/System.err﹕ at java.io.File.createNewFile(File.java:939) W / System.err:at java.io.File.createNewFile(File.java:939)

09-13 05:33:45.260 5867-5867/? 09-13 05:33:45.260 5867-5867 /? W/System.err﹕ at com.example.terabyte.tut1app.MainActivity.onCreate(MainActivity.java:78) W / System.err:at com.example.terabyte.tut1app.MainActivity.onCreate(MainActivity.java:78)

And this is my manifest: 这是我的表现:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

What could be the issue? 可能是什么问题?

Turns out I am fully capable of writing to the external storage directory on a real smartphone device (Huawei P8 api level 21, Version 5.0). 结果我完全有能力写入真实智能手机设备上的外部存储目录(华为P8 api level 21,版本5.0)。 But not to the emulator's external storage, even though the storage is fully available and can be browsed from within the emulator. 但不是模拟器的外部存储,即使存储完全可用并且可以在模拟器中浏览。

This also helped me in figuring out issues with the external storage on a real device: https://stackoverflow.com/a/6311955/5330055 这也帮助我找出了真实设备上外部存储的问题: https//stackoverflow.com/a/6311955/5330055

I have no longer urgently require a solution on this, but it would be nice to know what is wrong with the emulator. 我不再迫切需要一个解决方案,但知道模拟器有什么问题会很好。

Actually nothing wrong with emulator. 实际上模拟器没什么问题。

Probably device you have used on emulator were on android equal or higher than version 6.0 您在模拟器上使用的设备可能等于或高于6.0版本

In 6.0 and higher versions you need to request write permission at runtime. 在6.0及更高版本中,您需要在运行时请求写入权限。 WRITE_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE

in your smartphone it worked because it was on android 5.0 在你的智能手机它工作,因为它是在Android 5.0上

if this help someone I will glad. 如果这有助于某人,我会很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM