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

时间:2017-02-17 05:57:06

标签: java android csv android-external-storage file-writing

我遇到过网站上其他人已经存在的问题。我尝试过这些解决方案而且它们对我不起作用。

我目前正在开发一款可根据用户输入创建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());
    }

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

任何有关可能出现问题的帮助都会受到赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

您向MediaScanner发送广播的方式,但可能无法有效工作,也不建议使用。

建议的方法是添加已创建/更新的文件的文件路径,例如[在String类型ArrayList中]

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

现在您需要运行 MediaScannerConnection 类的scanFile()静态方法,传递包含所有文件列表的字符串数组已创建/更新,需要进行媒体扫描。

您还可以让侦听器在单个文件的扫描完成后作出响应。

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);

                }
            });

希望这能解决你的问题。

答案 1 :(得分:0)

供内部存储使用:

getFilesDir()getCacheDir()

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