I have an app that generates big output files. I am saving them into the "Download" folder of the internal storage and I want to retrieve them from the "Download" folder of the mounted phone storage on my PC.
The code I am using contains:
String filename="output.txt";
File dirname = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
FileWriter outFileWriter;
outFile = new File(dirname, filename);
outFileWriter = new FileWriter(outFile);
outFileWriter.append(outputString);
outFileWriter.close();
The phone is a Oneplus2 and the folder mounts on my Windows 7 as Computer\ONE A2001\Internal storage\Download
From the adb shell I can see and read the file, but the computer doesn't see it. When I copy a file from my computer into the mounted Download folder, I can see it there from the adb shell, so I know I am looking a the right place but the computer cannot see my app's output file.
ls -l
gives:
shell@OnePlus2:/storage/sdcard0/Download $ ls -l
ls -l
-rw-rw---- root sdcard_r 2030918 2015-09-11 17:24 copiedFromComputer.jpg
-rw-rw---- root sdcard_r 581632 2015-09-11 17:32 output.txt
So the file permissions are the same, but why can I not see my output.txt
file from the PC?
PS. Is there a better alternative to retrieve app generated files into the PC without using adb backup?
Alright, apparently a file needs to be indexed by the media scanner after its creation so it will be made available to access.
As in http://developer.android.com/reference/android/os/Environment.html one can call in the app:
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
or
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(outFile)));
or from the adb shell:
shell@shamu:/ $ am broadcast android.intent.action.MEDIA_MOUNTED
However if the file is modified after it has been indexed, the changes are not reflected to its metadata such as file size. Yet, if you download it to your computer the size and other metadata will be corrected.
Perhaps somebody can comment on how to update the metadata of an already indexed file after being modified on an Android system.