Android: getExternalFilesDir - Path not showing on PC

I think this is a silly mistake, but I cannot help myself here ... This is my first Android app. I am trying to save some data to an emulated SD card so that the user can get the data easily.

This class should write some DataFields on 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 am debugging an app on asus tablet. Everything works fine so far. The folder was first created and exists now. The file is written and can also be read programmatically. If I open the device with an Android device monitor, I see the files / mnt / shell / emulated / 0 / Android / data / my.package.name / files /. This was totally expected and I'm happy it works so smoothly. However, when I mount an Android device on my computer, my folder doesn't show up 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 when I choose the download folder as my target. Again I see the files when I browse the directory tree with the android device monitor (/ shell / emulated / 0 / Download /), but I don't see the files when I open the Download folder on the device on my computer.

What am I doing wrong? Isnt getExternalFilesDir should return folder visible on top of usb?

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

Edit2: I have solved the problem. Thanks :) 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 highly recommend reading this question

+3


source to share


1 answer


Isnt getExternalFilesDir should return folder visible on top of usb?

Yes, but only after the file in the folder has been indexed MediaStore

. You can request indexing with MediaScannerConnection

andscanFile()

.



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

+4


source







All Articles