Android 写入 SD 卡时 csv 文件不可见

Android csv file not visible when writing to SD card

我一直遇到网站上其他人已经遇到的问题。我已经尝试过这些解决方案,但它们对我不起作用。

我目前正在开发一款可根据用户输入创建 csv 文件的应用程序。当设备通过 USB 连接到 PC 时,用户需要能够检索该文件。保存代码功能正常,但连接设备后在 PC 上看不到文件。

这是应用的完整保存代码片段:

    FileWriter fileWriter = null;
    File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File dir = new File(sdCard.getAbsolutePath() + "/appFolder/");
    File dir2 = new File(sdCard.getAbsolutePath() + "/appFolder/appSubFolder/");
    dir.mkdirs();
    dir2.mkdirs();
    fileName = clientName + " " + agentName + ".csv";
    path = sdCard.getAbsolutePath() + "/appFolder/appSubFolder/";
    file = new File(dir2, fileName);

    dir.setReadable(true);
    dir.setWritable(true);
    dir.setExecutable(true);

    dir2.setReadable(true);
    dir2.setWritable(true);
    dir2.setExecutable(true);

    file.setReadable(true);
    file.setWritable(true);
    file.setExecutable(true);

    this.clientName = clientName;
    this.agentName = agentName;
    BufferedWriter bufferedWriter = null;
    try {
        if(!dir.exists()){
            dir.createNewFile();
            Toast.makeText(context,"dir created", Toast.LENGTH_SHORT).show();
        }
        if(!dir2.exists()){
            dir2.createNewFile();
            Toast.makeText(context,"dir2 created", Toast.LENGTH_SHORT).show();
        }
        if (!file.exists() )
        {
            file.createNewFile();
            Toast.makeText(context,"file created", Toast.LENGTH_SHORT).show();
        }
        /*
        fileWriter = new FileWriter(file, true);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(fullOrder);

        fileWriter.flush();
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
        */

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        outputStreamWriter.append(fullOrder);
        outputStreamWriter.close();
        fileOutputStream.close();

        MediaScannerConnection.scanFile(context, new String[]{file.toString()}, null, null);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(dir)));
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(dir2)));
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (IOException ioEx) {
        Toast.makeText(context, ioEx.toString(), Toast.LENGTH_SHORT).show();
        Log.d("mTag", "msg");
        Log.d("Exception ioEx 1", ioEx.toString());
    } catch (Exception ex) {
        Toast.makeText(context, ex.toString(), Toast.LENGTH_SHORT).show();
        Log.d("mTag", "msg");
        Log.d("Genereic ex saveToFile", ex.toString());
    }

文件正在写入外部存储,虽然我可以看到更多 space 已被占用,但文件本身仍然不可见。

如能就可能出现的问题提供任何帮助,我们将不胜感激。谢谢!

您向 MediaScanner 发送广播的方式可能效率不高,也不推荐。

推荐的方法是添加已经created/updated的文件的文件路径,像这样[在String类型的ArrayList]

ArrayList<String> filesToBeScanned = new ArrayList<String>();
filesToBeScanned.add(item.getFilePath());

现在你需要 运行 MediaScannerConnection class 和 pass 的 scanFile() 静态方法包含所有文件列表的字符串数组,这些文件created/updated需要进行媒体扫描。

您还可以设置一个侦听器,以便在对单个文件的扫描完成后做出响应。

String[] toBeScannedStr = new String[toBeScanned.size()]; toBeScannedStr = fileToBeScanned.toArray(toBeScannedStr);

            MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uri) {
                    System.out.println("SCAN COMPLETED: " + path);

                }
            });

希望这能解决您的问题。

用于内部存储:

getFilesDir()getCacheDir()

请参阅 android 文档:
https://developer.android.com/training/data-storage