kevinb kevinb - 5 months ago 9
Java Question

Android app creates file but can't find it

I'm having an issue that is preventing me from releasing my app, so maybe you can help. I have this code to create and write to a file:

@Override
public void onClick (View view){
final String memoryString = memory.getText().toString();


File file = new File(getApplicationContext().getFilesDir(), filename);

FileOutputStream fos = new FileOutputStream(file);
if (location != null) {
String s = location.toString() + " " + memoryString;
Log.d(null, s);
if(fos != null) {
Log.d(null, "fos not null");
fos.write(s.getBytes());
}else{Log.d(null, "Output Stream is null");}
fos.close();
Log.d(null, "file created!");

}
} catch (IOException e) {
e.printStackTrace();
}


my log is saying that it's creating the file. but when I go to access and read from said file, I get the error here that it doesn't exist:

try {
File memoryFile = new File(filename);

if(memoryFile.exists()){
revealMarkers(memoryFile);
Log.d(null, "revealed");
}else{Log.d(null, "no file found");}
} catch (FileNotFoundException ex) {
Log.d(null, "file not found");
}


Please help if you are able, I am very much stuck

Answer

Maybe this ?

try {
        File memoryFile = new File(getApplicationContext().getFilesDir(), filename);

        if(memoryFile.exists()){
                revealMarkers(memoryFile);
                Log.d(null, "revealed");
        }else{Log.d(null, "no file found");}
    } catch (FileNotFoundException ex) {
        Log.d(null, "file not found");
    }
Comments