【发布时间】:2017-11-19 16:31:52
【问题描述】:
我有一个应用程序必须接受一个字符串,将其编码为 JSONObject 格式并将其写入 SD 中的 JSON 文件。除了写作部分之外,这一切似乎都很好。我的清单中有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />command,这是我的代码
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;
}
当我按下按钮时,我会从 Toast 中收到此消息 “存储/模拟/0importantemail.json 没有权限” 不知道为什么会这样
【问题讨论】:
-
通常会出现此错误,因为您在 Manifest 中没有 WRITE_EXTERNAL _STORAGE 的权限,请查看链接developer.android.com/training/data-storage/files.html
-
嗨,对不起,我一定错过了提及它。我确实有它
标签: java android json bufferedreader