0

I'm trying to download a file from a server to the external sd card. Here's my code:

try {
            URL url = new URL(address);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            File SDCardRoot = Environment.getExternalStorageDirectory();
            File file = new File(SDCardRoot,"myfile.txt");

            FileOutputStream fileOutput = new FileOutputStream(file);

            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            int downloadedSize = 0;

            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            while ( (bufferLength = inputStream.read(buffer)) > 0 )
                fileOutput.write(buffer, 0, bufferLength);
                downloadedSize += bufferLength;
                publishProgress(downloadedSize, totalSize);

            }

            fileOutput.close();

The problem is, the app doesn't seem to be able to find the external sd card location:

03-20 13:03:29.615 16992-17244/com.example.myapp W/System.err: java.io.FileNotFoundException: /storage/emulated/0/myfile.txt: open failed: EACCES (Permission denied)

Is there some new permission that I need to add? I have the read and write external storage permissions added. Is there something that I'm missing? Any input is greatly appreciated!

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp" >

<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>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Alan Wang
  • 153
  • 2
  • 9
  • "I have the read and write external storage permissions added" -- please post your manifest. Also, what is your `targetSdkVersion`, and what version of Android are you testing on? "to the external sd card" -- [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) is not [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). – CommonsWare Mar 20 '16 at 18:07
  • @CommonsWare Thanks, I've edited in my manifest above. My targetSdkVersion is 23, and I'm testing on 23 as well. – Alan Wang Mar 20 '16 at 18:16
  • You are being affected by runtime permissions, most likely: http://stackoverflow.com/questions/32635704/cant-get-the-permission – CommonsWare Mar 20 '16 at 18:18

0 Answers0