简体   繁体   中英

External storage to internal storage

I am making a program that would save the inputted word in an EditText to a textfile in sd card. However, there is something wrong about mounting sd card in my tablet so im thinking of saving the text file to internal storage instead. Can anyone please help me how to switch this to internal storage? any comment would be greatly appreciated.thank you.

Here's my code:

public void writeToSDFile() {


    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root);    

    File dir = new File (root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "wordlist.txt");

    try {   


        FileOutputStream f = new FileOutputStream(file);           

        PrintWriter pw = new PrintWriter(f);
        pw.println(stringword);
        pw.append(stringword);        

        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found.");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\n\nFile written to "+file);    


}//end writeToSDFile

Since external storage is removable, you should not presume that it exists all the time. Hence before doing any io operation, check the storage state.

About your question to internal storage, There are two ways: 1. In application storage (app cache) - refer: Environment.getDataDirectory() 2. In common Data directory - refer: Context.getCacheDir().

Hope this helps.

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