0

In my android-app I have created a folder on the internal storage of my device. When I browse the internal storage I can see the folder and its content. Browsing through device

When I connect the device to my pc and open the internal storage, the folder is missing. All other folders are available, but not the folder I created from my app. Browsing through Windows-Explorer

I tried to turn on "show invisible files" for my windows, but the folder is still missing. Any suggestions?

I tried to use MediaScannerConnection.scanFile from Vijai's answere. After this, i can see the file, but is not displayed as a directorie. Nothing happens with double click on it.

enter image description here

The folder itself is beeing created by my databasemanager. Its an extensionclass of the SQLiteOpenHelper-class.

private DatabaseManager(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION); //DATABASE_NAME = Environment.getExternalStorageDirectory() + File.separator + "[Name of my directorie]" + File.separator + "[Name of my db].DB";
    mContext = context;
    getWritableDatabase();
}

This was working fine for so long. I was able to see the directory in the past. Since my customer changed his phone to android 6.0.1, the file is no more visible.

4

1 回答 1

2

To scan all the files in a folder, you can build a String array containing the file paths and pass it to the mediascanner's scanFile() method.

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) {

    File dir = new File(Environment.getExternalStorageDirectory(), "yourdirectory");

    // Create empty directory and its missing parent directories
    dir.mkdirs();

    if (dir.exists())
        MediaScannerConnection.scanFile(this, buildFilePaths(dir), null, null);
}

private String[] buildFilePaths(File directory){
    File[] files = directory.listFiles();
    ArrayList<String> filePaths = new ArrayList();
    for (File file : files)
        filePaths.add(file.getAbsolutePath());
    return filePaths.toArray(new String[filePaths.size()]);
}

-----Original answer-----

You have to rescan the storage to show the directory (android does a whole system media scan at intervals or during boot)

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) {

    File dir = new File(Environment.getExternalStorageDirectory(), "yourdirectory");

    // Create empty directory and its missing parent directories
    dir.mkdirs();

    if (dir.exists())
        MediaScannerConnection.scanFile(this, new String[] {dir.toString()}, null, null);
}

p.s: MTP mode sucks!

于 2018-02-13T11:49:41.323 回答