简体   繁体   中英

How to write file to sd-card? (Android) No errors, but nothing written

I'm new to developing android apps. And already overchallenged with my first project. My app should be able to save a list of EditText fields to a text file by clicking a "save"-Button. But I got no success to write a file to my SD-card.

My code: (function in MainActivity.java called by the button)

public void saveData(View view){
        try{
        File sdcard = Environment.getExternalStorageDirectory();
        // to this path add a new directory path
        File dir = new File(sdcard.getAbsolutePath() + "/myapp/");
        // create this directory if not already created
        dir.mkdir();

        // create the file in which we will write the contents

        File file = new File(dir, "datei.txt");


        FileOutputStream os = new FileOutputStream(file);
        String data = "some string";
        os.write(data.getBytes());
        os.flush();
        os.close();
        }
        catch (IOException e){
            Log.e("com.sarbot.FitLogAlpha", "Cant find Data.");
        }
    }

With Google I found another way:

public void saveData3(View view){
    FileWriter fWriter;
    File sdCardFile = new File(Environment.getExternalStorageDirectory() + "/datafile.txt");
    Log.d("TAG", sdCardFile.getPath()); //<-- check the log to make sure the path is correct.
    try{
         fWriter = new FileWriter(sdCardFile, true);
         fWriter.write("CONTENT CONTENT UND SO");
         fWriter.flush();
         fWriter.close();
     }catch(Exception e){
              e.printStackTrace();
     }
}

In my manifest.xml I set the permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And the function from developer guide returns me True -> SD-card is writable.

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

In the res/layout/activity_main.xml are some TextViews and EditText and a save button with android:onClick="saveData" argument. The function is called. The SD-card is writable. And no IO errors. But after pressing the button (without error) there is still no new file on my SD-card. I already tried to create the file manually and just append but nothing changed. I tried some other function with BufferedWriter too .. but no success.

I'm running my Sony Xperia E with USB-Debug mode. Unmount and mounted the SD-card on my PC but cant find the file. Maybe it is only visible for the phone? It doesn't exist? I don't know what to do because I get no errors. I need the content of this file on my computer for calculations.

:EDIT: The problem was not in the code.. just in the place I looked up. The externalStorage -> sdCard seems to be the internal and the removable sdcard is the -> ext_card.

os.flush() is missing in your code. Add this snippet before os.close()

After this line,

 File file = new File(dir, "datei.txt");

Add this code

 if ( !file.exists() )
 {
      file.createNewFile();         // This line will create new blank line.
 }

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