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" />