duncanportelli
duncanportelli

Reputation: 3229

Where do files get saved in Android?

I am developing an application which writes some JSON data in a text file:

FileOutputStream fOut;
fOut = openFileOutput("myfile.txt", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(myJSONData);

I am testing this application on the device and when I plug it in my computer, I am not able to see the file. Can you please tell me under which directory is it stored and do I need to set the operating mode of the file to MODE_WORLD_READABLE in order to see it please?

Upvotes: 0

Views: 5726

Answers (4)

moof2k
moof2k

Reputation: 1897

To save files to internal storage on Android (as of SDK 23) get your application's file storage location using getFilesDir() and save them there. The files should then be saved to /data/user/0/your.app.name/files. See: Saving Files.

File filename = new File(getFilesDir(), "filename.txt");

Once written, you can retrieve this file from your computer using adb:

adb pull "/data/user/0/your.app.name/files/filename.txt"

Upvotes: 0

waqaslam
waqaslam

Reputation: 68167

It saves the file in /data/app_package_name/files folder. This location is inaccessible to user through file managers - only your app can access it through code.

Besides, if you are really eager to access the private files then you need to consider the option of rooting your device.

Upvotes: 3

Austin B
Austin B

Reputation: 1600

Plugging it into your computer is mounting it to the SD card, usually. To save to that, get the SD card root directory File with Environment.getExternalStorageDirectory() and then construct a new File with that File and your desired string filename as the arguments. That will save it to your SD card which should be the first folder you see when you mount it on your computer.

By the way, for writing text/JSON straight to a file, just use FileWriter.

Upvotes: 2

Ajay S
Ajay S

Reputation: 48592

You can not see the internal files visually until mobile is root. If you want to see it then store it in SD card.

Upvotes: 1

Related Questions