I have this function and when I call it, it won't return anything.
public String getfileFromSDCard(String filename){//get the file
String text;
text = "";
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File file = new File(root,"file.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
text = text + str;
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
return text;
}
Do I need to add any permission to my android manifiest other than
...uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ... ( I worte this line right in the code)
I have this one added already and running cause I can easily write a file, but I cant read it afterward
This is how I'm calling the function from the onCreate
TextView countDisplay = new TextView(this);
CreatefiletoSDCard("testnumberone.txt","22222fkdf sdjf hjsdf sdj fsdf ");
countDisplay.setText(getfileFromSDCard("testnumberone.txt"));
this.setContentView(countDisplay);
ps: CreatefiletoSDCard() works.
Although you pass in a filename
parameter in your getfileFromSDCard()
method, you seem to have hardcoded the actual file(name) you're reading from to file.txt
. Guessing by the filenames you use for creating your test files, the error is probably that you're trying to read from a non-existent file, no matter what name you actually pass into the method.
In other words, you probably want to change the following line:
File file = new File(root,"file.txt");
to:
File file = new File(root,filename);