samjaf
samjaf

Reputation: 1053

Android: getExternalFilesDir - Path is not visible on PC

I guess this is a stupid mistake, but I can't help myself here... This is my very first Android-App. I'm trying to save some data on the emulated sd-card so the user can easily retrieve the data.

This class is supposed to write some DataFields to the sd-card

    private final List<DataField> dataFields;

    private final File exportFile;
    private DateFormat simpleDate = new SimpleDateFormat("yyyyMMddhhmm");

    private CsvExporter(List<DataField> dataFields) {
        this.dataFields = dataFields;
        exportFile = new File(getTargetFolderPath(), getTargetFileName());
    }

    private String getTargetFileName() {
        return "export_" + (simpleDate.format(new Date())) + ".csv";
    }

    private File getTargetFolderPath() {
        //return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File targetFolder = getExternalFilesDir(null);
        if (!targetFolder.exists()){
            if (!targetFolder.mkdirs()){
                Log.e("CsvWriter","Could not create targetFolder " + targetFolder);
            }}
        Log.d("CsvWriter","TargetFolder is " + targetFolder);
        for (File f: targetFolder.listFiles()){
            Log.d("CsvWriter","Found file " + f);
        }
        return targetFolder;
    }

    private void initFile() throws IOException {
        if (!exportFile.exists()) {
            if (!exportFile.createNewFile()){
                Log.e("CsvWriter","Could not create targetFile " +exportFile);
            }
        }
        Log.d("CsvWriter","targetFile is " +exportFile);
    }


    public File doExport() {
        try {
            initFile();
            List<String[]> data = convertToStrings();
            writeData(data);
            readData();
            return exportFile;
        } catch (IOException e) {
            throw new RuntimeException("Unable to create or write to csv-File", e);
        }
    }

I'm debugging the app on an asus tablet. Everything works fine so far. The folder was created the first time and does exist now. The file is written and also can be read programatically. If I open the device with the android device monitor i can see the files /mnt/shell/emulated/0/Android/data/my.package.name/files/. This was totally expected and I'm happy this runs so smooth. Nevertheless, when i mount the android-device on my pc, my folder is not visible under /Android/data/my.package.name/files

I have android.permission.WRITE_EXTERNAL_STORAGE included in my AndroidManifest.xml

You can see a different approach where i choose the download folder as my target. Again i can see the files when browsing the directory tree with android device monitor (/shell/emulated/0/Download/) but cant see the files when I open the Download folder on the device on my pc.

What am I doing wrong? Isnt getExternalFilesDir supposed to return a folder visible over usb?

Edit: Here is a screenshot of my adm and my filebrowser... Screenshot if my adm and my filebrowser.

Edit2: I solved the problem. Thanks :) The fixed doExport():

        public File doExport() {
        try {
            initFile();
            List<String[]> data = convertToStrings();
            writeData(data);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + getTargetFolderPath())));
            //readData();
            return exportFile;
        } catch (IOException e) {
            throw new RuntimeException("Unable to create or write to csv-File", e);
        }
    }

I strongly recommend to read this question

Upvotes: 4

Views: 2622

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

Isnt getExternalFilesDir supposed to return a folder visible over usb?

Yes, but only after a file in the folder has been indexed by the MediaStore. You can request that it be indexed using MediaScannerConnection and scanFile().

For more information, I have a blog post specifically about this situation.

Upvotes: 4

Related Questions