Open failed: EACCES (Permission denied) in Android

4.7k views Asked by At

I found a code and edited a bit according to my application. It is about writing a text to txt on Android in bytes. I'm getting this error, Exception 'open failed: EACCES (Permission denied)' on Android

As stated in the link above, I changed its place where user-permission related to write_external_storage is located in my Android Manifest file. However, in that file, I received " tag appears after tag" Warning leading me to get the same error again.

Thank you in advance

 if (response.equals("tag OK " + param[2]
                + " authenticated (Success)")) {

             mySuccessfulLogin = param[0] + "\n"
                    + param[1] + "\n" + param[2]
                    + "\n" + newpassword;

            myDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Users/macbookpro/Documents/CS328/Android_IMAP";

            File custdir = new File(myDirectory);
            if(!custdir.exists())
                {
                    custdir.mkdirs();

                }

            File savedir=new File(custdir.getAbsolutePath());
            File file = new File(savedir, "LastLogin");

            if(file.exists())
                 {
               file.delete();
                 }

            FileOutputStream fos;

            byte[] data = mySuccessfulLogin.getBytes();

            try {

                fos = new FileOutputStream(file);
                fos.write(data);
                fos.flush();
                fos.close();
                               // Toast.makeText(, "File Saved", Toast.LENGTH_LONG).show();

             } catch (FileNotFoundException e) {
                                //Toast.makeText(getBaseContext(), "Error File Not Found", Toast.LENGTH_LONG).show();
               Log.e("fnf", ""+e.getMessage());
                                // handle exception
              } catch (IOException e) {
                                // handle exception
                               // Toast.makeText(getBaseContext(), "Error IO Exception", Toast.LENGTH_LONG).show();
              }

        }
1

There are 1 answers

5
Lukos On BEST ANSWER

You need this permission in your manifest

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

before the application tag.