Android API 23 mkdir 创建的文件夹在 windows 资源管理器中显示为单个文件

Android API 23 mkdir created folder is shown as single file in windows explorer

所以基本上我用 mkdir 创建了一个文件夹,然后将一个简单的文本文件写入该文件夹。作为最后一步,我使用 MediaScannerConnection.ScanFile 使它们对文件资源管理器可见。

文件夹和文本文件都可以在我的 android phone.When 上的文件资源管理器中看到,使用 MTP i 通过 USB 将 phone 连接到我的 windows 10 计算机查看所有其他文件夹,但我新创建的文件夹显示为单个文件,没有扩展名,大小为 4KB。

重新启动 Smartphone 和计算机无法解决问题,但是当我在 android 文件资源管理器中重命名文件夹然后重新启动 phone 它显示为普通文件夹windows 探索者。

应用程序显示一个对话框,点击一个按钮后,有一个文件列表可供选择覆盖,编辑文本输入新文件名,否定按钮和肯定按钮。

这里是代码:

 /*
Save Config in a File stored in interal storage --> which is emulated as external storage 0
Shows a Dialog window with the option to select a file which is already there or type in a name for a new file or choose from a list of files 
--> save the new file or cancel the dialog
*/
public void savebutton(View view) {

        try {
            // Instantiate an AlertDialog with application context this
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //External Storage storage/emulated/0/Config --> storage/emulated/0 is internal storage emulated as sd-card
            dir = new File(Environment.getExternalStorageDirectory() + folder);
            if (!dir.exists()) {
                if (!dir.mkdir()) { //create directory if not existing yet
                    Log.d(MainActivity.LOG_TAG, "savebutton: directory was not created");
                }
            }

            //set title of dialog
            builder.setTitle("Save Configuration in Text File");
            //Add edittext to dialog
            final EditText input = new EditText(ConfigActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            input.setImeOptions(EditorInfo.IME_ACTION_DONE);        // instead of a RETURN button you get a DONE button
            input.setSingleLine(true);                              // single line cause more lines would change the layout
            java.util.Calendar c = java.util.Calendar.getInstance();
            int day = c.get(java.util.Calendar.DAY_OF_MONTH);
            int month = c.get(java.util.Calendar.MONTH) + 1;
            int year = c.get(java.util.Calendar.YEAR);
            String date = Integer.toString(day) + "." + Integer.toString(month) + "." + Integer.toString(year);
            String savename = name + "-" + date;
            input.setText(savename);

            //Append term + "=" + value --> like LevelOn=50
            for (int itemcounter = 0; itemcounter < value.length; itemcounter++)
                datastring += (term[itemcounter] + "=" + getvalue(itemcounter) + "\n");

            //File List of already stored files for dialog
            mFilelist = dir.list();
            builder.setItems(mFilelist, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    mChosenFile = mFilelist[which]; // the item/file which was selected in the file list

                    try {
                        //Create text file in directory /Pump Config Files
                        File file = new File(dir.getAbsolutePath(), mChosenFile);
                        //create bufferedwrite with filewriter
                        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                        //write to file --> an existing file will be overwritten
                        bw.write(datastring);
                        //Flush bufferedwriter
                        bw.close();
                        //LOG
                        System.out.println("Wrote to file");
                        //Show a message
                        Toast.makeText(getApplicationContext(), "Saved data", Toast.LENGTH_SHORT).show();

                        // initiate media scan -->cause sometimes created files are nt shown on computer when connected to phone via USB
                        // restarting the smartphone or rename folder can help too
                        // make the scanner aware of the location and the files you want to see
                        MediaScannerConnection.scanFile(
                                getApplicationContext(),
                                new String[]{dir.getAbsolutePath()},
                                null,
                                new MediaScannerConnection.OnScanCompletedListener() {
                                    @Override
                                    public void onScanCompleted(String path, Uri uri) {
                                        Log.v("LOG", "file " + path + " was scanned successfully: " + uri);
                                    }
                                });
                    } catch (Exception e) {
                        System.out.print(e.toString());
                    }
                    //dismissing the dialog
                    dialog.cancel();
                    dialog.dismiss();
                }
            });

            // Get the AlertDialog from builder create()
            AlertDialog dialog = builder.create();
            //Set dialog view/layout
            dialog.setView(input);

            //Add positive button to dialog
            //When pressing the positive button a file with the specified name and configurtion is stored on internal storage
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        filename = input.getText().toString();
                        //Create text file in directory /Pump Config Files
                        File file = new File(dir.getAbsolutePath(), filename + ".txt");
                        //create bufferedwrite with filewriter
                        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                        //write to file --> an existing file will be overwritten
                        bw.write(datastring);
                        //Flush bufferedwriter
                        bw.close();
                        //LOG
                        System.out.println("Wrote to file");
                        //Show a message
                        Toast.makeText(getApplicationContext(), "Saved data", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        System.out.print(e.toString());
                    }
                    // initiate media scan -->cause sometimes created files are nt shown on computer when connected to phone via USB
                    // restarting the smartphone or rename folder can help too
                    // make the scanner aware of the location and the files you want to see
                    MediaScannerConnection.scanFile(
                            getApplicationContext(),
                            new String[]{dir.getAbsolutePath()},
                            null,
                            new MediaScannerConnection.OnScanCompletedListener() {
                                @Override
                                public void onScanCompleted(String path, Uri uri) {
                                    Log.v("LOG", "file " + path + " was scanned successfully: " + uri);
                                }
                            });

                    dialog.cancel();
                    dialog.dismiss();
                }
            });

            //Add negative button
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            //show the save dialog
            dialog.show();
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }

在 Android API 23 和 API 21 上测试。 21 日可以正常工作,但 23 日不行。

感谢 greenapps,

新字符串[]{dir.getAbsolutePath()},.您应该改为扫描文件:new String[]{file.getAbsolutePath()},

工作正常。

我总是遗漏一些像这样的小细节。