martes, 15 de febrero de 2011

Android: write EASY on internal storage

Recently I found that not all sdcards filesystem root paths starts with /sdcard. So I was getting an Exception trying to write files in a location that doesn't exist. Epic fail in my code.

Filesystem root path independent code

String content = "Hola mundo";       
FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();

This will create a file on your application private space, so no other applications can access to it.

Great thing is that you don't need to know anything about paths, permissions, inputstreams, ... Just let Android handle it for you.

No hay comentarios:

Publicar un comentario