程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-創建Android外部存儲器文件問題

java-創建Android外部存儲器文件問題

編輯:編程綜合問答
創建Android外部存儲器文件問題

我想創建一個.txt文件並將其存儲在Android手機的外部存儲器。我增加了Android Manifest權限。我運行代碼時不給出任何錯誤提示,但文件卻創建不了。我不知道代碼哪裡出錯,請高人指點,謝謝。

public void createExternalStoragePrivateFile(String data) {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(myContext.getExternalFilesDir(null), "state.txt");

    try {

        FileOutputStream os = null; 
        OutputStreamWriter out = null;
        os = myContext.openFileOutput(data, Context.MODE_PRIVATE);
        out = new OutputStreamWriter(os);
        out.write(data);
        os.close();

        if(hasExternalStoragePrivateFile()) {
            Log.w("ExternalStorageFileCreation", "File Created");
        } else {
            Log.w("ExternalStorageFileCreation", "File Not Created");
        }

    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

最佳回答:


File file = new File(myContext.getExternalFilesDir(null), "state.txt");
try {

     FileOutputStream os = new FileOutputStream(file, true); 
     OutputStreamWriter out = new OutputStreamWriter(os);
         out.write(data);
     out.close();
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved