-2

I'm trying to save a file to external storage, however it's giving the FileNotFoundException error.

Note: I've already made the changes to Android Manifest.

        File file = null;
        if (isExternalStorageWritable()) {
          Log.d("SEND", "Entrou no IF do ExernalStorage");
          //File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
          File path = ContaListFragment.this.getActivity()
                                     .getApplicationContext()
                                     .getExternalFilesDir(
          Environment.DIRECTORY_PICTURES);//getExternalFilesDir
          file = new File(path, "file.json");

          //Make sure the Pictures directory exists.
          Log.d("SEND", "" + path.mkdir() + " PATH=" + file.getAbsolutePath());
          //file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "file.json");
        }

        if (!file.mkdirs()) {
           Log.d("SEND", "Directory not created");
        }

        if (!isExternalStorageWritable()) {
           file = new File(ContaListFragment.this.getActivity()
                    .getApplicationContext().getFilesDir(), "file.json");
           Log.d("SEND", "Passei do File");
           output = new BufferedWriter(new FileWriter(file));
           Log.d("SEND", "Passou do Buffered");
           output.write(jsonObject.toString());
           uri = Uri.fromFile(file);
        }
sf_
  • 1,138
  • 2
  • 13
  • 28
Rafael Oliveira
  • 129
  • 1
  • 11

1 Answers1

0

The issue was resolved with the help of: See the answers to this question

The problem was that I was creating a directory instead of a file. Just replace: file.mkdirs(); with file.createNewFile();

Rafael Oliveira
  • 129
  • 1
  • 11