0

I am trying to create a simple text file in my android app using the code:

            FileOutputStream fileout=mContext.openFileOutput("mytextfile.txt", mContext.MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
            outputWriter.write(String.valueOf(executionTime));
            outputWriter.close();
            Log.d("write","Done writing to 'mysdfile.txt'");

and I log the different file paths as:

            String absoluteFilePath=new File(".").getAbsolutePath();
            String canonicalFilePath=new File(".").getCanonicalPath();
            String filePath=new File(".").getPath();
            Log.d("absolutepath",absoluteFilePath);
            Log.d("canonicalpath",canonicalFilePath);
            Log.d("path",filePath);

and in the debugger I see the file paths as:

enter image description here

When I click on the absolute path blue mark, it took me to the Macintosh HDD folder. The remaining two paths didn't make any sense to me.

I also checked my current directory and the file is not in there. I am trying to store the file in internal storage.

I am testing this on lollipop device.

Note: I see the file created in DDMS when the app is run on emulator, but not on device.

Where can I find the file that is created by FileOutputStream ?

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • "Where can I find the file that is created by FileOutputStream ?" -- it will be on the Android device or emulator. "and I log the different file paths as" -- none of those are valid paths on Android. – CommonsWare Mar 01 '16 at 16:09
  • My android device doesnt have an SD card. Where can I check them on device ? – Teja Nandamuri Mar 01 '16 at 16:10

1 Answers1

1

Where can I find the file that is created by FileOutputStream ?

It will be on the Android device or emulator.

and I log the different file paths as

None of those are valid paths on Android.

My android device doesnt have an SD card

That's fine, as openFileOutput() has nothing to do with an an SD card on any Android device. openFileOutput() routes to internal storage.

Where can I check them on device ?

Quoting myself:

On an emulator, DDMS has the ability to browse all of internal storage, so you can access file from your app or any other app.

On a rooted device, there are recipes for allowing DDMS the same degree of freedom.

On ordinary (un-rooted) devices, DDMS has no ability to access internal storage. The piece of software on the device that DDMS talks to runs as an ordinary user account on hardware, whereas it runs with superuser privileges on an emulator. The ordinary user on hardware has no ability to get to your app’s files, any more than does any user account associated with any other app.

The recipe for getting at internal storage on hardware is to use the run-as option with adb at the command line. For example, to download a database from the primary user’s internal storage to your development machine, you might use:

adb shell 'run-as your.application.package.name cp /data/data/your.application.package.name/databases/dbname.db /sdcard

Note that:

  • You will need to change the destination to wherever on your device external storage is mapped to (shown here as /sdcard/, which will not work on all devices)

  • You may need to use cat instead of cp on older devices

But, once you run the command, the database will be copied to external storage, which you can access via DDMS, or via any conventional way to get files on and off a device via USB cable (e.g., drive letter in Windows).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491