2

I created a system application, in it I'm trying to save a text file on my SD Card, I wrote the following code:

     Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

        if(isSDPresent)
        {

            try {
                File myFile = new File("/sdcard/mysdfile.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter =
                        new OutputStreamWriter(fOut);
                myOutWriter.append("tttttttteeeeeest");
                myOutWriter.close();
                fOut.close();
                 Toast.makeText(getBaseContext(),
                       "Done writing SD 'mysdfile.txt'",
                     Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                 Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            }//

        }
        else
        {
            Toast.makeText(getBaseContext(),"There is no SD Card",Toast.LENGTH_LONG).show();
        }

It's giving me the following error:

open failed eacces (permission denied)

This is the beginning of manifest file:

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

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

any help ?

Monzer Yaghi
  • 1,300
  • 1
  • 10
  • 21
  • 1
    First, **never hardcode paths**. Always use methods to derive paths (e.g., `getExternalFilesDir()`). Second, `/mnt/sdcard` is not [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) on most Android devices. Third, the permission is `READ_EXTERNAL_STORAGE`, not `read_external_storage`. Fourth, if your `targetSdkVersion` is 23 or higher, and you are testing on Android 6.0 and higher, [those permissions are `dangerous` and have to be requested at runtime](https://commonsware.com/blog/2015/08/31/hey-where-did-my-permission-go.html). – CommonsWare Oct 09 '15 at 13:15
  • Post your complete AndroidManifest, or at least the beginning of it, so we can be sure what your problem is. Also, _read the links_ CommonsWare posted. – Jonas Czech Oct 09 '15 at 13:16

1 Answers1

1

The app is system app, so you have no permission to access the SD.

In this case you have to create another app that has permission to SD, and use intent calls to read/write to SD.