createNewFile java.io.Ioexception: open failed: EACCES

Asked

Viewed 748 times

0

I am trying to create an excel file in a public directory with my app but am getting the following error:

java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:939)
at br.com.fexus.fretecif.MainActivity$1.onClick(MainActivity.java:57)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at java.io.File.createNewFile(File.java:932)
... 10 more

java.io.FileNotFoundException: /storage/emulated/0/Download/Frete&Cif.xls: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at br.com.fexus.fretecif.MainActivity$1.onClick(MainActivity.java:109)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission      denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 12 more

I cannot create or open the file (in this case because it was not created). I have already added the following code to manifest.xml:

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

For more information follow the code snippet in my Mainactivity:

boolean criou = true;
Workbook workbook = new HSSFWorkbook();
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
file = new File(folder, "Frete&Cif.xls");

try {
    if(!file.exists()) {
        file.createNewFile();
    }
} catch (IOException e) {
        e.printStackTrace();
}

Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("Frete&Cif"));

FileOutputStream output = null;

try {

    output = new FileOutputStream(file);
    workbook.write(output);

} catch (Exception e) {
    e.printStackTrace();
    criou = false;
} finally {
    if (output != null) {
       try {
          output.close();
          workbook.close();
       } catch (IOException e) {
          e.printStackTrace();
       }
    }
    System.out.println(criou);
}

Manifest.xml:

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.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"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>
  • Fernando, you could post your entire Manifest or at least where you are declaring these permissions?

1 answer

2


Since the Android version of Kitkat, apps cannot store data in the SD Card as they used to. You can read the data, but you can only write data in your own dedicated directory. That is, there is no way you can write in the /extSdCard/Downloads folder/

  • So how would you store in the directory itself in Kikat versions onwards? And more important because there are methods for these build versions without appearing that has been deprecated or something like that? Thank you for the information.

  • Hi, could you try the following code? File file = new File(Environment.getExternal?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.