-2

This is my first Android application I'm making, so I'm a bit lost on what the terms are for everything.

I'm trying to copy a file I have in my /raw/ directory to the root of my SD card.

Currently, my (used Stackoverflow, didn't write this fully myself) code looks like this:

btnWriteSDFile.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            try {
                File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "fileName.docx");
                myFile.createNewFile();
                Toast.makeText(v.getContext(),"Wrote line", Toast.LENGTH_SHORT).show();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
                myOutWriter.append("testFile");
                myOutWriter.close();
                fOut.close();
                Toast.makeText(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
            }
            catch (Exception e)
            {
                Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }


    });

I get the error: "open failed; EACCES (Permission Denied)".

My Manifest looks like this:

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

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

</application>

Clarification to avoid duplication: Besides the fact that I get this error: how can I write a /raw/file.docx file to my SD card root?

Joris Borza
  • 51
  • 1
  • 9
  • 1
    In addition to having `` in the wrong place (per the answer), you also need to take into account [runtime permissions](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). – CommonsWare Aug 07 '17 at 12:14
  • 1
    Possible duplicate of [Exception 'open failed: EACCES (Permission denied)' on Android](https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android) – Alastair McCormack Aug 07 '17 at 12:14
  • Thanks, I'll include that as well! I do wonder though, how do I write my /raw/file.docx to the SD card? – Joris Borza Aug 07 '17 at 12:17

1 Answers1

3

According to this documentation you have to add the uses permission inside and immediate to <manifest> tag

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permisson.READ_EXTERNAL_STORAGE" />
    ...
    <application>
        ...
        <activity> 
            ...
        </activity>
    </application>
</manifest> 

like this

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

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

</application>
</manifest>

this should work for copying file

InputStream in = getResources().openRawResource(R.raw.fileNameYourGoingToCopy);
String path = Environment.getExternalStorageDirectory() + "/anyFolder" + File.separator + fileNameWithExtension;
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
    while ((read = in.read(buff)) > 0) {
        out.write(buff, 0, read);
    }
} finally {
    in.close();
    out.close();
}
Vinujan.S
  • 1,199
  • 14
  • 27
  • Thanks! This removes the error for the location, but how do I write my /raw/file.docx to the directory? – Joris Borza Aug 07 '17 at 12:14
  • 1
    @clanc I have edited. Check whether it works. It is a duplicate of this answer. https://stackoverflow.com/a/19465224/5202960 – Vinujan.S Aug 07 '17 at 12:32