I know, this question has been asked a thousand times, but no solution worked for me... I simply want to save data into a file on the SD card. But first, here's my code:
private static final String TAG="Logmessage";
public void save(){
String state=Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
File dir=new File(Environment.getExternalStorageDirectory() + "/NewFolder");
if(!dir.exists()){
if (dir.mkdirs()){
Log.i(TAG,"Dir made");
}else{
Log.i(TAG,"Error: Dir not made");
}
}else{
Log.i(TAG,"Dir already exists");
}
File file=new File(dir,"file.txt");
try {
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
bWriter.write("Hello World!");
bWriter.close();
}catch (IOException e){
Log.e("Exception","BufferedWriter");
}
}else{
Log.i(TAG,"External Drive not available");
}
}
Of course I added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the manifest file, but when I run the app, the log messages are always: Logmessage: Dir not made
and Exception: BufferedWriter
.
So I suppose, the entry in the manifest file didn't "work", because it couldn't even create the directory?