5

I want to create a new file in external storage if that file doesn't exist already. I've already read similar questions in SO and have WRITE_EXTERNAL_STORAGE permission in my manifest. Testing on GenyMotion emulator with android 5.1 and xperia t with android 4.3 the result is same and I get "open failed: EACCES (Permission denied)" on file.createNewFile(). I checked in runtime and getExternalStorageState functoin return value is "MOUNTED".

Note: If I create the file manually, my code works perfectly and reads the content meaning that accessing to external storage is OK. I although write to external storage another place in my code using getExternalPublicStorage for saving captured image and it works fine!

File f = Environment.getExternalStorageDirectory();
File config = new File(f, "poinila config" + ".txt");
if (!config.exists()) {
    if (!config.createNewFile()) {
        // toast that creating directory failed
    } else {
        writeDefaultIpPort();
    }
}

Edit: path string is "/storage/sdcard0/poinila config.txt"

Alireza Farahani
  • 1,570
  • 3
  • 21
  • 44

3 Answers3

2

well I think this is the new security feature introduced in android. Runtime permissions. You have to do something like this

           int hasReadExternalStoragePermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
            if(hasReadExternalStoragePermission != PackageManager.PERMISSION_GRANTED)
                if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
                    new AlertDialog.Builder(getContext())
                            .setMessage("You need to Allow access to Images to set a Company Logo")
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    requestPermission();
                                }
                            }).show();
                    else
                        requestPermission();
            else
                callChooser();
neelabh
  • 471
  • 6
  • 18
0

Is your permission is right place in manifest file as

  <application>
        ...

    </application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    </manifest>
Vaibhav Barad
  • 615
  • 7
  • 17
0

I had the same issue. I think it is a missing / in the path. Try this:

config = new File(Environment.getExternalStorageDirectory() + "/" +  "poinila config.txt" );

Hope it helps

ya man
  • 312
  • 4
  • 10
  • Could you post the complete stack trace? Also the Path-String? – ya man Sep 09 '15 at 16:48
  • Strangly, the stacktrace isn't printed in console! But I will attach an image when catching exception in debug mode. Path string added to question – Alireza Farahani Sep 10 '15 at 09:46
  • All the Exceptions `cause` fields as far as I saw were "libcore.io.ErrnoException: open failed: EACCES (Permission denied)" – Alireza Farahani Sep 10 '15 at 09:51
  • I still thinks this is a path issue. Try it on /storage/emulated/0/. This should refer to the internal "sd-card". – ya man Sep 10 '15 at 10:58
  • I've tried that (on both emulator and a real device) and result is the same – Alireza Farahani Sep 10 '15 at 12:35
  • This is my stack trace: 09-10 16:09:51.668: W/System.err(10511): java.io.FileNotFoundException: /storage/emulated/0test.log: open failed: EACCES (Permission denied) – ya man Sep 10 '15 at 14:11
  • as you can see there is a missing /. I don't know why I can't add the / there. I think you should use the StreamReader/Writer method: http://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android?lq=1 – ya man Sep 10 '15 at 14:12