I found lot of topics with the same problem, but they couldn't fix mine. I initially write a file as follow:`
我发现了很多同样的问题,但是他们不能解决我的问题。我最初写的文件如下:
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File notefile = new File(root, sFileName);
FileWriter writer = null;
try {
writer = new FileWriter(notefile);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
writer.append(sBody);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
writer.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Don't worry about the try and catch blocks, i will clear them later :D. And this is the reader which should works in the same directory ("Notes" of the sdcard, if it doesn't exist, will be created), read the file, and put it on a Notify as you can see:`
别担心,试着抓块,我以后会把它们清除的。这是应该在同一个目录下工作的阅读器(如果不存在,将创建sdcard的“Notes”),读取该文件,并将其放在一个通知中,您可以看到:
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root, "Nota.txt");
//Read text from file
text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close() ;
}catch (IOException e) {
e.printStackTrace();
}
I really don't understand why i get this problem, i even try with
我真的不明白我为什么会有这个问题,我甚至试过。
getExternalStorageDirectory().getAbsolutePath()
but without success.
但没有成功。
Can someone help me?
有人能帮助我吗?
3 个解决方案
#1
0
You've tried to check with a debugger if root exists when you do:
当你做的时候,你试着用一个调试器来检查:
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
I do not think it will create the folder when you write the file
我不认为它会在你写文件的时候创建文件夹。
#2
0
To write a file in external storage, you need to have WRITE_EXTERNAL_STORAGE permission enabled.
要在外部存储中编写文件,需要启用WRITE_EXTERNAL_STORAGE权限。
Why are you trying to write a file in external storage?
为什么要在外部存储中编写文件?
I mean if you want this file for your app only means, use context.getExternalDirs() to get your app's sandbox, it doesn't require write permission, above android 4.2(Jelly bean).
我的意思是,如果你想要你的应用程序的这个文件只是意味着,使用context.getExternalDirs()来获取你的应用的沙箱,它不需要写权限,在android 4.2(Jelly bean)之上。
If you want to share the file to other apps, you're doing the right job.
如果你想把这个文件分享给其他应用,你的工作做得很好。
And before writing the file, check whether external storage is mounted programmatically.
在编写文件之前,检查外部存储是否以编程方式安装。
#3
0
Problem Solved
I used SharedPreferences of Android. I stored the data in MainActivity and take in in my Class as follow:
我使用了Android的SharedPreferences。我将数据存储在MainActivity中并在我的类中接收如下:
-
MainActivity
MainActivity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = prefs.edit(); editor.putString("string_id", InputString); //InputString: from the EditText editor.commit();
SharedPreferences首选项= PreferenceManager.getDefaultSharedPreferences(这个);SharedPreferences。编辑器编辑= prefs.edit();编辑器。putString(“string_id”,InputString);//InputString:来自EditText editor.commit();
-
In my Class to get my data`
在我的课堂上获取我的数据
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String data = prefs.getString("string_id", "no id"); //no id: default value
SharedPreferences首选项= PreferenceManager.getDefaultSharedPreferences(这个);字符串数据=首选项。getString(" string_id”、“id”);/ /没有id:默认值
.android.com/reference/android/content/SharedPreferences.html
.android.com/reference/android/content/SharedPreferences.html
#1
0
You've tried to check with a debugger if root exists when you do:
当你做的时候,你试着用一个调试器来检查:
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
I do not think it will create the folder when you write the file
我不认为它会在你写文件的时候创建文件夹。
#2
0
To write a file in external storage, you need to have WRITE_EXTERNAL_STORAGE permission enabled.
要在外部存储中编写文件,需要启用WRITE_EXTERNAL_STORAGE权限。
Why are you trying to write a file in external storage?
为什么要在外部存储中编写文件?
I mean if you want this file for your app only means, use context.getExternalDirs() to get your app's sandbox, it doesn't require write permission, above android 4.2(Jelly bean).
我的意思是,如果你想要你的应用程序的这个文件只是意味着,使用context.getExternalDirs()来获取你的应用的沙箱,它不需要写权限,在android 4.2(Jelly bean)之上。
If you want to share the file to other apps, you're doing the right job.
如果你想把这个文件分享给其他应用,你的工作做得很好。
And before writing the file, check whether external storage is mounted programmatically.
在编写文件之前,检查外部存储是否以编程方式安装。
#3
0
Problem Solved
I used SharedPreferences of Android. I stored the data in MainActivity and take in in my Class as follow:
我使用了Android的SharedPreferences。我将数据存储在MainActivity中并在我的类中接收如下:
-
MainActivity
MainActivity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = prefs.edit(); editor.putString("string_id", InputString); //InputString: from the EditText editor.commit();
SharedPreferences首选项= PreferenceManager.getDefaultSharedPreferences(这个);SharedPreferences。编辑器编辑= prefs.edit();编辑器。putString(“string_id”,InputString);//InputString:来自EditText editor.commit();
-
In my Class to get my data`
在我的课堂上获取我的数据
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String data = prefs.getString("string_id", "no id"); //no id: default value
SharedPreferences首选项= PreferenceManager.getDefaultSharedPreferences(这个);字符串数据=首选项。getString(" string_id”、“id”);/ /没有id:默认值
.android.com/reference/android/content/SharedPreferences.html
.android.com/reference/android/content/SharedPreferences.html