2

I need create a file available to other apps, for example .txt; I tried many ways, but nothing helped; File is created, but the file manager (e.g total commander) doesn't see it; I have this :

String filename = mTitle.getText().toString().trim() + ".txt";
        String string = mContent.getText().toString().trim();
        FileOutputStream outputStream;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
        file.mkdirs();
        file.setReadable(true);
        try {
            outputStream = openFileOutput(file.getName(), MODE_WORLD_READABLE);
            outputStream.write(string.getBytes());
            outputStream.close();
            Toast.makeText(getApplicationContext(), file.getAbsolutePath(), Toast.LENGTH_LONG).show();
            MediaScannerConnection.scanFile(
                    getApplicationContext(),
                    new String[]{file.getAbsolutePath()},
                    null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.v("grokkingandroid",
                                    "file " + path + " was scanned seccessfully: " + uri);
                        }
                    });
            new SingleMediaScanner(this, file);
        } catch (Exception e) {
            e.printStackTrace();
        }


I ordered all permissions. Thanks!

Georgi
  • 23
  • 5

1 Answers1

0

Replace:

openFileOutput(file.getName(), MODE_WORLD_READABLE);

with:

new FileOutputStream(file);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I try. Now, this problem: java.io.FileNotFoundException: /storage/emulated/0/Download/titlexgxgzrRZZTZ.txt: open failed: EACCES (Permission denied). all permissions: WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE written – Georgi Jul 03 '16 at 15:05
  • @Georgi: Try http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Jul 03 '16 at 15:29