0

I have this in my manifest:

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

This is where I am trying to create and write to a file (it is in a file called EnterUserInfo.java:

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};


/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        System.out.println("INSIDEEEEEE");
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    } else {
        System.out.println("HEREEEEEEEEE");
    }
}

private void writeToFile(String data, Context context) {

    verifyStoragePermissions(this);
    String FILENAME = "new_clients.txt";
    String string = "hello world!";

    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        FileOutputStream fos = context.openFileOutput("new_clients.txt", Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

When I try to create a file, this is what appears:

I/System.out: HEREEEEEEEEE
W/ContextImpl: Failed to ensure /data/user/0/c.b.project/files: mkdir failed: EACCES (Permission denied)
W/FileUtils: Failed to chmod(/data/user/0/cs.b07.cscb07courseproject/files): android.system.ErrnoException: chmod failed: EACCES (Permission denied)
W/System.err: java.io.FileNotFoundException: /data/user/0/c.b.project/files/new_clients.txt (Permission denied)
W/System.err:     at java.io.FileOutputStream.open(Native Method)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err:     at android.app.ContextImpl.openFileOutput(ContextImpl.java:506)
W/System.err:     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:192)
W/System.err:     at EnterUserInfo.writeToFile(EnterUserInfo.java:69)

As you can see, it prints here meaning the permission is granted, but right after it gives a Permission Denied error. Any idea how to solve this?

Edit: On a side note, when it says that it tries to save to /data/user/0/cs.b07.cscb07courseproject/files, is that within the project or is that saved on my computer? Because when I go to my terminal and do cd /data/ or cd /data neither is found.

Edit: writeToFile() is called in the same class and file posted above, and this is the code (the function below is called when a user hits the "register" button in the UI:

public void createNewUser(View view) {
    // a data string is created here:
    // String data = "asd";
    writeToFile(data, this);
}

Edit 2: Please note that I did ask for permission at runtime in my verifyStoragePermissions() method. Unless something is wrong with that way of asking for permission (which I don't think it is because a prompt does appear which asks the user for permission), then I think the issue is with something else.

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • you are using marshmallow device? – Divyesh Patel Nov 30 '16 at 06:24
  • @Divyesh I'm using Android 7.0 – SilentDev Nov 30 '16 at 06:27
  • 2
    http://stackoverflow.com/questions/33030933/android-6-0-open-failed-eacces-permission-denied – Divyesh Patel Nov 30 '16 at 06:27
  • you don't show where you call your `writeToFile`. I suspect it is called in wrong place, before you approve permissions – Vladyslav Matviienko Nov 30 '16 at 06:32
  • @Divyesh I looked at your link. I do ask for permission right before I try to write (not just on install / in my manifest file). – SilentDev Nov 30 '16 at 06:32
  • try to save your file in another location and check. – Divyesh Patel Nov 30 '16 at 06:36
  • @Divyesh I can't change the location of where the file is saved (it is already pre-set.. when I call the `context.openFileOutput` function it saves it to `context.getFilesDir()` which is `/data/user/0/cs.b07.cscb07courseproject/files`. – SilentDev Nov 30 '16 at 06:38
  • then i think that folder is not accesible. Have to tried to save file in that location? – Divyesh Patel Nov 30 '16 at 06:41
  • @Divyesh Is the `/data` folder when I called `context.getFilesDir()` on my computer or in the android project directory? Because I cannot find it in the project directory, so I checked my computer and I do need to use `sudo` to write to `/data`. From my understanding, requesting the user for permission and having write permissions granted handled that part. – SilentDev Nov 30 '16 at 06:49
  • data folder is private for that app, you should use File appdir= new File(Environment.getExternalStorageDirectory(),".YOUR"); – Divyesh Patel Nov 30 '16 at 06:52
  • Can you change `Manifest.permission.WRITE_EXTERNAL_STORAGE` to `"android.permission.WRITE_EXTERNAL_STORAGE"` and check if it is works? – HendraWD Nov 30 '16 at 07:13
  • @VladMatvienko I edited my post to show where `writeToFile` is called. – SilentDev Nov 30 '16 at 22:44
  • You do not need any permissions to call `openFileOutput()`. This writes a file to the private application-specific data area, which is owned by your application. Are you getting this error on an emulator or a real device? – David Wasser Dec 02 '16 at 17:04

3 Answers3

3

You do not need any permissions to call openFileOutput(). This writes a file to the private application-specific data area, which is owned by your application.

Judging by these errors:

W/ContextImpl: Failed to ensure /data/user/0/c.b.project/files: mkdir failed: EACCES (Permission denied)
W/FileUtils: Failed to chmod(/data/user/0/cs.b07.cscb07courseproject/files): android.system.ErrnoException: chmod failed: EACCES (Permission denied)

It looks like someone has changed the file ownership (or access rights) on your application's private data directory /data/user/0/c.b.project/. This directory should be owned by your application's user ID and therefore your application should have the necessary rights to write to it.

Uninstall your app (which should delete that directory) and then reinstall your app (which should recreate the directory with the correct permissions).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

Requesting Permissions at Run Time

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app.

More about runtime permission
Refer Answer

Community
  • 1
  • 1
Mable John
  • 4,518
  • 3
  • 22
  • 35
  • right, so what do I do with my code to solve this issue? Because I do call `verifyStoragePermissions()` right before using the permission and it does say that the permission is granted. – SilentDev Nov 30 '16 at 06:29
0

Hi firstly you have to check which android SDK version you are using

if it is less than 23 than you just have to put your permission in manifest file it work

if android version greater than 23 you should put all permission in manifest file as well as you should ask user permission for run time ( only first attempt )
for this you should follow this link https://stackoverflow.com/a/33162451/4741746

One more thing you can don is to change compileSdkVersion and buildToolsVersion to below 23 like 22 (but i will not suggest you for this because new features above 23 you can not be use )

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

   defaultConfig {
      minSdkVersion 16
      targetSdkVersion 23
   }
} 

if not working let me know

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55