Android FileOutputStream not creating files

1.1k views Asked by At
package org.jaberrio.personai2;
import android.content.Context;
import android.widget.Toast;
import java.io.FileOutputStream;

public class DataBaseManager {

    public void setDataBase(Context context) {
        String fileName = "CoolDataBaseFile.txt";
        String inputText = "Random Text Goes Here";
        FileOutputStream outputStream;
        try {
            outputStream = context.openFileOutput(fileName, context.MODE_PRIVATE);
            outputStream.write(inputText.getBytes());
            outputStream.close();
            Toast finishedLoad = Toast.makeText(context, "I Have Finished Loading", Toast.LENGTH_SHORT);
            finishedLoad.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

Now I am trying to create a file however when I call this constructor the Toast notifies me that it ran but no file is created. I looked for it in data/data/ but my directory for the app is not even there.

Also I am calling the constructor from a fragment like this:

DataBaseManager db = new DataBaseManager();
        db.setDataBase(getActivity().getApplicationContext());
1

There are 1 answers

0
CommonsWare On BEST ANSWER

On a Physical Device

You do not have access to internal storage on an actual device through normal tools. The primary exception is if you root the device.

adb shell run-as allows you to run shell commands as if they were being run your app, and so in principle you could use that to examine internal storage and see if the file is there.

The simpler solution is to use the emulator, particularly the x86 emulator, as you can browse internal storage on the emulator, using DDMS (in Eclipse) or the Android Device Monitor (launched from Android Studio).