简体   繁体   中英

Android FileOutputStream not creating files

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());

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM