我嘗試保存一個txt文件,我的文件夾中,在內部存儲,但每次我面臨同樣的問題:節省內部文件中的Android
「源未找到」
我以不同的方式編寫我的代碼,如下所示,但在所有方面我都有同樣的問題。
值得一說,我甚至在的Manifest.xml這是沒有必要的內部存儲添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
。
這是不用說,我沒有任何問題,保存在/數據/數據/包/文件路徑的文件,但是當我加入我的文件夾中的文件的根例如/數據/ data/package/files/myforlder/myfile.txt我面對「未找到資源」問題。
你能指出我正確的方向來解決這個問題嗎?
第二個問題是,將文件保存在外部存儲的外部文件夾中。
(例如:SD卡或USB存儲器)是不同的還是相同的?
第一種方式:
OutputStreamWriter out;
try {
File path=new File(getFilesDir(),"myfolder");
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
out = new OutputStreamWriter(openFileOutput(mypath.getAbsolutePath() , MODE_PRIVATE));
out.write("test");
out.close();
}
}
方式二:
OutputStreamWriter out;
try {
ContextWrapper cw = new ContextWrapper(this);
File path = cw.getDir("myfolder", Context.MODE_PRIVATE);
if (!path.exists()) {
path.createNewFile();
path.mkdir();
}
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
out = new OutputStreamWriter(openFileOutput(mypath.getAbsolutePath() , MODE_PRIVATE));
out.write("test");
out.close();
}
}
第三條道路:
File path=getFilesDir();
String mypath=path.toString() + "/myfolder";
OutputStreamWriter out;
try {
File f = new File(mypath , "/myfile.txt" );
out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE));
out.write("test");
out.close();
}
第四道:
File path=getFilesDir();
OutputStreamWriter out;
try {
File f = new File(path.getPath() + "/myfolder/myfile.txt" );
out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE));
out.write("test");
out.close();
}
第五方式:
File path=getFilesDir();
OutputStreamWriter out;
try {
File f = new File(path.getCanonicalPath() + "/myfile.txt");
out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE));
out.write("test");
out.close();
}
在編寫問題時選擇代碼並按ctrl + k,這會自動縮進和着色代碼,這樣更具可讀性。沒有人會想要閱讀那些沒有縮進和無色的代碼。 – 2011-04-23 20:16:13