Reputation: 44345
Writing some text to a file on an android device seems to be a major endeavor. Starting from an answer given here I have implemented the following code into my single 'Hello-World' activity:
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write("lalala");
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
which does not throw an exception, but seems to work. But is there a way to 'see' the file created with the File Manager
on android? The code snippet seems to write to a 'secret' location on the android file system related to the application itself (governed by using this.openFileOutput
).
Consulting different google-links (this and this and this) I come up with the following code:
File file = new File(this.getExternalFilesDir("temp"), "testfile.txt");
FileOutputStream fileOutput = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);
fileOutput.write("lalala");
fileOutput.close();
which throws an error
Error:(55, 19) error: no suitable method found for write(String) method FileOutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method FileOutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method OutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(byte[]) is not applicable (actual argument String cannot be converted to byte[] by method invocation conversion)
So how to do this right?
As a side note: This is for debugging/educational purposes only and not intended to be part of a final app!
To be clear: I want to create a file inside the temp
directory I can see with a FileManager (the temp
directory is on the same level as My Documents
, Music
, DCIM
, Download
and the like...)
Upvotes: 3
Views: 8121
Reputation: 1006849
But is there a way to 'see' the file created with the File Manager on android?
I am not certain what "the File Manager" is that you are referring to, as Android does not really have one. However, you are writing to an app's internal storage, and that is private to the app. It cannot be viewed by other apps or ordinary users, except on rooted devices and via some cumbersome Android SDK tools.
So how to do this right?
File file = new File(this.getExternalFilesDir(null), "testfile.txt");
FileOutputStream fileOutput = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutput);
outputStreamWriter.write("lalala");
outputStreamWriter.flush();
fileOutput.getFD().sync();
outputStreamWriter.close();
Also:
Please do this work on a background thread
On Android 4.3 (API Level 18) and older devices, you need to hold the WRITE_EXTERNAL_STORAGE
permission to work with external storage
If you also want to have this file show up quickly in on-device or on-development-machine file managers, use this after closing the file:
MediaScannerConnection.scanFile(
this,
new String[]{file.getAbsolutePath()},
null,
null);
(where this
is some Context
, such as an Activity
or Service
)
Upvotes: 7