0

i am trying to save a file in the emulator with the name that choose it the user and the current date .

but i get an error message that say :open failed :EACCES (Permission denied )

how to fix this error i will appreciate any help

SingInSActivity.java

public class SignSoldgerActivity extends Activity {

    EditText edit_txt_note;
    final Context context = this;
    // attribute for the date picker

    public String fileName;

    Button btn_save_soldger;
    TextView txtatePicker;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_soldger);

        edit_txt_note = (EditText) findViewById(R.id.editTxtNote);


        txtatePicker = (TextView) findViewById(R.id.txtDate);




        btn_save_soldger = (Button) findViewById(R.id.btnSaveSoldger);
        btn_save_soldger.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // / for creating a dialog
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);

                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // get user input and set it to result
                                        // edit text
                                        String userinputResult = userInput
                                                .getText().toString();

                                        SimpleDateFormat formatter = new SimpleDateFormat(
                                                "yyyy/MM/dd_HH:mm:ss");
                                        Date now = new Date();
                                        fileName = formatter.format(now) + "__"
                                                + userinputResult;

                                        txtatePicker.setText(fileName);
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

                // / for saving the file on the SD

                try {
                    String sdPath = Environment.getExternalStorageDirectory()
                            .getAbsolutePath() + fileName + ".txt";
                    File myFile = new File(sdPath);
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter myOutWriter = new OutputStreamWriter(
                            fOut);
                    // append or write
                    myOutWriter.append(edit_txt_note.getText());
                    myOutWriter.close();
                    fOut.close();
                    edit_txt_note.setText("");
                    Toast.makeText(getBaseContext(),
                            "Done Writing SD" + fileName, Toast.LENGTH_SHORT)
                            .show();

                } catch (Exception e) {

                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

            }

        });
    }

the permission is added in the

manifest file

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

user3006788
  • 165
  • 1
  • 14
  • 30

2 Answers2

1

Please add following in your manifest:

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

Most likely you need to read as well so this is needed for that:

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

Also note that most likely SD card has different allowed characters so you might need to replace all "|\\?*<\":>+[]/'"; with wanted character like _

Niko
  • 8,093
  • 5
  • 49
  • 85
  • What is the filename you are trying to write? Could there be illegal characters? – Niko Dec 24 '13 at 08:33
  • the user choose the file name after an alert dialog pop up and ask him to enter a file name then the system will add **2 under Score** to the name and the current date and time that has a format **"yyyy/MM/dd_HH:mm:ss"** – user3006788 Dec 24 '13 at 08:38
  • http://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android You can see from here which are allowed characters. – Niko Dec 24 '13 at 08:40
1

Emulator SD Card size should be defined

You need to edit your AVD to allot an SD card size to it. You need to launch the AVD Manager dor it of course.

Here's the screen where you add it:

Edit AVD

Updating after to include your comments

You're mostly missing the path seperator.

String sdPath = Environment.getExternalStorageDirectory()
    .getAbsolutePath() + fileName + ".txt";

must be changed to

String sdPath = Environment.getExternalStorageDirectory()
    .getAbsolutePath() +"/"+ fileName + ".txt";
Community
  • 1
  • 1
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
  • i am using the genymotion emulator and this is the specifications of the emulator **Memory Size:2048 MB**, **android version 4.3**, **Data storage capacity: 16384 MB** – user3006788 Dec 24 '13 at 09:27
  • I've not used genymotion before. I'd wager that data storage capacity is for internal storage. Their docs/manuals may yield more information. – Dheeraj Bhaskar Dec 24 '13 at 09:29
  • i tried now on the avd of the google and create an avd with SD size 40 MB and when i tried to start and save the file an new error had appear : **Open failed : EROFS (Read-only file system)** – user3006788 Dec 24 '13 at 09:40
  • Please open a new question for the new error you have. I'm guessing you are trying to write somewhere on the FS where you don't have write perms. Accept/upvote my answer if it helped you. – Dheeraj Bhaskar Dec 24 '13 at 09:46