简体   繁体   中英

Android Studio path

Hey Guys I did a little App, where I type into a textbox a specific value (height, weight) and save it into a file. I did this but I do not know, which path I have to use for Android.

Hope you can help :)

 public void SaveList(View view) {
        //Pf`enter code here`ad, im privaten Speicherbereich
        File file = new File("I need this path :)");
        try {
            OutputStreamWriter fdg = new OutputStreamWriter(new FileOutputStream(file));
            fdg.write(""+this.weight);
            fdg.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

You can use Environment.getDataDirectory() to get the root directory, if you dont have an SD card.

If you have an SD card, use Environment.getExternalStorageState()

Read more about them in the docs

Thus, change your code as follows

File file = new File(Environment.getDataDirectory()+"/your_folder_name/your_file_name");

This will create a file with name your_file_name in the folder your_folder_name in your internal storage.

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