Android como verificar se o arquivo existe e então criar u
Tenho a seguinte pergunta. Gostaria de colocar um arquivo chamado data.xml na pasta sdcard / appname e usá-lo para ler e gravar dados do aplicativo.
ntão, quando minha atividade principal é criada, preciso verificar se esse arquivo exist
public class appname extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.no_elements_l);
File file = getBaseContext().getFileStreamPath("/sdcard/appname/data.xml");
if(file.exists()) {
return;
} else {
// create a File object for the parent directory
File MTdirectory = new File("/sdcard/appname/");
// have the object build the directory structure, if needed.
MTdirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(MTdirectory, "data.xml");
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream DataFile = new FileOutputStream(outputFile);
}
Mas eu tenho o tipo de exceção Unhandled FileNotFoundException na última linha. Qual é o problema? Usa a permissão WRITE_EXTERNAL_STORAGE é adicionada ao manifesto.