How can I see the file I have created on my Android mobile ?
I created an Android application with Android Studio. Everything is working well, I can save and find the files I save with the file explorer. However, when I created my APK, I can't find the file. The strange thing is that I see the directory I have created, but it is empty (there should be .csv files).
What I've tried :
I use Fragments in my application. I created a write method, a permission one and called them in my onCreateView method. I have enable permission in AndroidManifest.xml. I use mediaStore methods, from How to write files to external public storage in Android so that they are visible from Windows? but it don't seems to work. I call the scanFile method just after I have created my .csv file (inside the write method).
The code :
AndroidManifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
final LayoutInflater lf = getActivity().getLayoutInflater();
final View rootView = inflater.inflate(R.layout.frag_layout, container, false);
// Permissions to write
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
}
// some code
saveTextAsFile("litchi_mission", myText);
}
Write method:
// Write and save file
private void saveTextAsFile(String filename, String myText){
String fileName = filename + ".csv";
//create file
File file = new File(getActivity().getExternalFilesDir("mission"), fileName);
//write file
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(myText.getBytes());
fos.close();
Toast.makeText(getActivity(), "Saved to :\n" + getActivity().getExternalFilesDir("mission") + "/" + fileName, Toast.LENGTH_LONG).show();
scanFile(getActivity().getLayoutInflater().getContext(), file, "text/csv");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
Permission method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode){
case 1000:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(getActivity(), "Permission granted!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Permission denied!", Toast.LENGTH_LONG).show();
getActivity().finish();
}
}
}
MediaStore scanFile method:
public void scanFile(Context ctxt, File f, String mimeType) {
MediaScannerConnection.scanFile(ctxt, new String[] {f.getAbsolutePath()}, new String[] {mimeType}, null);
}
Results :
File explorer finds the created csv file
Moreover, the message appearing when I save my file is still storage/emulated/0/... despite I am using my own phone with an APK (so there should be no emulator folder, right ?).
Logcat
Here is the logcat from the emulated device :
2019-07-31 11:21:10.144 1913-1913/? W/WindowManager: removeWindowToken: Attempted to remove non-existing token: android.os.Binder@a7c62c8
2019-07-31 11:21:53.438 1676-4342/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 3987322 , only wrote 3987152
2019-07-31 11:21:53.466 1696-1873/? D/AF::Track: interceptBuffer: took 211us to intercept 0 tracks
2019-07-31 11:21:53.516 1913-1940/? E/memtrack: Couldn't load memtrack module
2019-07-31 11:21:53.517 1913-1940/? W/android.os.Debug: failed to get memory consumption info: -1
2019-07-31 11:21:53.638 1913-1940/? E/memtrack: Couldn't load memtrack module
2019-07-31 11:21:53.638 1913-1940/? W/android.os.Debug: failed to get memory consumption info: -1
2019-07-31 11:21:53.732 11493-12002/? W/d.process.medi: JNI critical lock held for 19.558ms on Thread[16,tid=12002,Runnable,Thread*=0xe0e33e00,peer=0x12d405e0,"AsyncTask #1"]
2019-07-31 11:21:53.788 1686-1796/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 868352
2019-07-31 11:21:53.813 11493-12002/? D/MediaProvider: Scanned /storage/emulated/0/Android/data/com.example.flightio/files/mission/litchi_mission.csv as /storage/emulated/0/Android/data/com.example.flightio/files/mission/litchi_mission.csv for null
What is in red :
2019-07-31 11:20:33.596 1913-1940/? E/memtrack: Couldn't load memtrack module
2019-07-31 11:20:33.745 11493-11493/? E/d.process.medi: Not starting debugger since process cannot load the jdwp agent.
2019-07-31 11:31:51.907 1913-1944/? E/BluetoothAdapter: Bluetooth binder is null
2019-07-31 11:31:51.948 1913-1944/? E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)
2019-07-31 11:32:04.687 1913-2051/? E/ClipboardService: Denying clipboard access to com.google.android.as, application is not in focus neither is a system service for user 0
2019-07-31 11:32:07.593 1838-1838/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2019-07-31 11:32:07.595 1838-1838/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
Edit
After some test, the method saveTextAsFile() seems the origin of these errors. I have made a simple test to write a txt file, but it doesn't work on my real device. It works on the emulated device, but it display the same errors in the logcat.
Furthermore, every code I have tested to write a file doesn't works on a real device...