I have an app that has to take a string, encode it into JSONObject format and write it into a JSON file in the SD. It all seems to be working fine apart from writing part. I have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
command in my MANIFEST and that's my code
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// String lines[] = importantemail.split("\\r?\\n");
// String firstLine = (lines[0]);
//String secondLine = (lines[1]);
// Toast.makeText(SignificantEmailActivity.this,firstLine + secondLine,Toast.LENGTH_SHORT).show();;
JSONObject jsonObject = makeJsonObject();
try{
Writer output = null;
File file = new File(Environment.getExternalStorageDirectory()+ "importantemail.json");
if (!file.exists()) {
file.mkdirs();
}
output = new BufferedWriter(new FileWriter(file));
output.write(jsonObject.toString());
output.close();
Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
finish();
}
});
}
public JSONObject makeJsonObject()
{
JSONObject object = new JSONObject();
try {
object.put("Message ID", id);
object.put("Sender",accountStr);
object.put("Subject",subj);
object.put("E-mail:",importantemail);
}catch (JSONException e)
{
e.printStackTrace();
}
return object;
}
When I press the button I get this message from the Toast "storage/emulated/0importantemail.json Permission denied" No idea why is that though