0

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 :

Folder created

But the csv file is not saved

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...

jegertor
  • 73
  • 2
  • 8
  • Replace `fos.close();` with `fos.flush(); fos.getFD().sync(); fos.close();` and see if that helps. Right now, not everything is necessarily flushed to disk by the time you are requesting the scan. Beyond that, try a third-party file manager, and it may be that your file manager has issues (e.g., caches `MediaStore` results and therefore is not detecting the newly-indexed file). – CommonsWare Jul 30 '19 at 20:28
  • **storage/emulated/0** does not mean you are running from an emulator. [Read more here](https://android.stackexchange.com/questions/205430/what-is-storage-emulated-0) – Giddy Naya Jul 30 '19 at 20:33
  • Thank you for your answers. I have tried to change `fos.close` but it is still not working. I will try a third-party file manager, but it seems complex (I am new in Android developement). – jegertor Jul 30 '19 at 21:15
  • When you tried saving, was your device connected to the computer? Did you try the APK after disconnecting device from computer? – Zohaib Amir Jul 30 '19 at 21:30
  • Indeed, my phone was pluged to my computer. But now there is another strange things : **1** when I connect my phone to my PC, I can click on my save button. The path is shown (with a Toast message) but the file is **not saved** (can't find it on my device, see *results photos* on my post). **2** When I disconnect the phone but **don't close immediately** the app, nothing change. Same Toast message but can't find the file. **3** However when I am not connected to my PC, and **restart the application**, when I click on my save button the app crash. So there is a big mistake in my code. – jegertor Jul 30 '19 at 21:50
  • Can you add your crash log of the app to the question? It might help to better understand your issue. – MD Naseem Ashraf Jul 31 '19 at 06:40
  • I have updated my post and added the logcat from the ***emulated device***. I don't know how to have access to the logcat / report on my real phone when I test the application. – jegertor Jul 31 '19 at 09:28

0 Answers0