1

I am creating a file and writing it like this:

                outputStream = context.openFileOutput(symbol, context.MODE_PRIVATE);
                outputStream.write(DATA.getBytes());
                outputStream.close();

I can read the file from the app but I can't see the files in explorer. I need to make it visible in explorer so that it can be shared(for debugging). Also, I need the file to be in readable format (like txt) for the computer. Also, files needs to be stored in internal directory. How can I do it?

Lcukerd
  • 438
  • 6
  • 21
  • `openFileOutput` I suggest you read the documentation for that method. – njzk2 May 08 '17 at 16:38
  • What is "explorer"? Do you mean a file manager for a desktop OS? – CommonsWare May 08 '17 at 16:39
  • @CommonsWare The file explorer of android. – Lcukerd May 08 '17 at 16:41
  • @njzk2 I know it says there private file but check here it says other app can read it.https://developer.android.com/training/basics/data-storage/files.html . If you any other way then tell me. – Lcukerd May 08 '17 at 16:43
  • @Lcukerd no it doesn't. `Context::openFileOutput` uses *Internal storage*, which according to the documentation : "Internal storage: [...] Files saved here are accessible by only your app.". You probably want to use external storage. – njzk2 May 08 '17 at 16:50

2 Answers2

2

To have your file be visible to other applications ("The file explorer of android"):

Step #1: Write to what the Android SDK refers to as external storage, such as getExternalFilesDir()

Step #2: Arrange to have the file indexed by the MediaStore, in case your file manager is using the MediaStore instead of the filesystem

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

For that you need to create a folder in external storage public directory.

Write this in manifest

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Then you have to define a public folder in the external storage as follows :

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
         File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    }

Here you can use DIRECTORY_PICTURES or any thing like DIRECTORY_DOCUMENTS or any thing as you wish. You can even place your own public directory instead of DIRECTORY_PICUTRES.The use of this DIRECTORY_PICTURES is that all your files saved in this folder will be read as pictures by the system.

So be careful while working with these

Bharat
  • 1,044
  • 15
  • 34
  • I get file not found error. open failed: ENOENT (No such file or directory) – Lcukerd May 08 '17 at 17:18
  • From where are you trying to open the file ? – Bharat May 08 '17 at 17:20
  • Well I am writing from WakefulBroadcastReceiver , and I am getting the error while writing using your code – Lcukerd May 08 '17 at 17:22
  • I don't have idea about WakefulBroadcastReciever you can refer this for detailed explanation on how storage works : https://developer.android.com/guide/topics/data/data-storage.html#filesExternal – Bharat May 08 '17 at 17:32
  • Well, it worked with Pictures directory but not documents. I dunno why / – Lcukerd May 08 '17 at 17:45