1

I am trying to access a file that i fill thanks to my app. I am storing it on my phone's memory, but not on my app's internal memory.

My problem is that the file doesn't exist when I open it on my PC (with phone connected via USB). But it is available and not empty when I open it directly from my smartphone.

I need this file to be available on my computer.

On my manifest:

READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE

I have tried this way:

FileWriter outFile = new FileWriter(Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
PrintWriter out = new PrintWriter(outFile);
out.append("text");
out.flush();
out.close();

And also this way:

 File file = Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
 FileOutputStream outputStream = context.openFileOutput(file, Context.MODE_WORLD_READABLE);
 PrintStream printStream = new PrintStream(outputStream);
 printStream.println(text);
 printStream.close();

Another way..

File file = Environment.getExternalStoragePublicDirectory("my_app/data/file.txt");
FileWriter outFile = new FileWriter(file);
BufferedWriter buf = new BufferedWriter(outFile);
buf.write("hey");
buf.close();
outFile.close();

Some of those solutions work, but only on my phone so it isn't world readable, others just don't work...

What is the difference between OutPutStreamWriter, FileWriter, BufferedWriter, FileOutputStream, PrintWriter, ...? Which one do I have to choose?

How do I manage to create my public file in order to access it directly from my computer?

Thank you for your help!

Anne-Claire
  • 872
  • 8
  • 16

1 Answers1

0

Try this:

String toWrite = "save me to disk";
File dir = new File(Environment.getExternalStorageDirectory(), INSTALLATION_DIRECTORY); 
if (!dir.exists()) {
    dir.mkdirs();
}
File file = new File(dir, INSTALLATION_FILE);
FileOutputStream out = new FileOutputStream(file );
out.write(toWrite.getBytes());
out.close();
Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • no it still doesn't work. My file is created and the text is written on my phone, but when i try to see this same file with my PC, the folder is empty... – Anne-Claire Sep 03 '13 at 08:05
  • This method has been tested by me and it works. So maybe the problem lies with the pc connection. Do you see the file using the ADB shell? – Itai Hanski Sep 03 '13 at 08:12
  • Yes I can see the file in Android Debug Monitor, but I can't see it in my file explorer. – Anne-Claire Sep 03 '13 at 08:55
  • short search yielded these results, try what they suggest: http://stackoverflow.com/questions/7391432/unable-to-see-file-in-windows-explorer-while-it-is-visible-in-android-file-brows http://stackoverflow.com/questions/7429087/cant-see-a-file-in-windows-written-by-an-android-app-on-sd-card-unless-i-force – Itai Hanski Sep 03 '13 at 09:08
  • It works for me with this: `context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+"my_path"))));` Thank you for your time! – Anne-Claire Sep 03 '13 at 09:26