Reputation: 499
I am writing an application where I would like to allow the created csv file to be used on a PC. Android has Internal Storage which is viewable on the Android and can be accessed via USB on the PC. I tried various methods to generate a file name:
/data/user/0/com.ed.ilan.ioioterbium/files/documents/2016_9_16_10_28_12.csv
/data/user/0/com.ed.ilan.ioioterbium/files/2016_9_16_10_35_57.csv
2016_9_16_10_46_34.csv
The name of the application I got using getFilesDir(), but the first 2 examples didn't work as Java complained about the slashes in the name. The 3rd one worked but nothing was visible in (appName)/files/. I don't know if this by design for security reasons, or if the file was really not there. The code is
String name = getFileName();
try {
FileOutputStream fOut = new FileOutputStream(name);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("test1");
osw.flush();
osw.close();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
Where I would really like the file to be would be in Internal Storage. I created a folder called myData, which I can see both on the phone and on the PC. The question is: how do I direct the application to write to Internal Storage. I recognize that this isn't "secure", but I just want to get the file.
To write to the Internal storage one needs this in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
If we should decide to put the file on a cloud server, is there a standard way to do that?
Thanks, Ilan
Upvotes: 0
Views: 1127
Reputation: 2403
If you are using getFilesDir() then it returns a path which is specific to android application. User's simply can't access the path returned by getFilesDir().
If you have a rooted device then you can get access to the above path.
If you want that only your application can access files stored by you then use getFilesDir()
, else you can use
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
Upvotes: 2
Reputation: 1587
I understand that you want to write a file to your phone's Documents
folder, am I right? If so, the following may be of help where you can write your .csv files at.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
Upvotes: 0