简体   繁体   中英

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. 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/? I/System.out﹕ /storage/0CED-0F09/Pictures

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

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

With an IOException (No such file or directory):

09-13 05:33:45.260 5867-5867/? W/System.err﹕ java.io.IOException: open failed: ENOENT (No such file or directory)

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

09-13 05:33:45.260 5867-5867/? 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). 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

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

In 6.0 and higher versions you need to request write permission at runtime. WRITE_EXTERNAL_STORAGE

in your smartphone it worked because it was on android 5.0

if this help someone I will glad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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