Write to JSONObject to JSONfile

2019-08-22 10:29发布

问题:

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

回答1:

Tested, Hi replace few lines and this will fix your issue.

  JSONObject jsonObject = makeJsonObject();
    try{
        Writer output = null;
        File file = new File(Environment.getExternalStorageDirectory(), "importantemail.json");
        if(file.isDirectory()){
            file.delete();
        }
        if(!file.exists()){
            file.createNewFile();
        }
        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", 5);
        object.put("Sender","test");
        object.put("Subject","test");
        object.put("E-mail:","test");
    }catch (JSONException e)
    {
        e.printStackTrace();
    }
    return object;
}


回答2:

in 6.0 or later versions of android, you must give runtime permission. check this.