【问题标题】:Android csv file not visible when writing to SD card写入 SD 卡时,Android csv 文件不可见
【发布时间】:2017-02-17 05:57:06
【问题描述】:

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

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

这是应用的完整保存代码sn-p:

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

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

任何可能出现问题的帮助将不胜感激。谢谢!

【问题讨论】:

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


    【解决方案1】:

    您向 MediaScanner 发送广播但可能无法有效工作的方式,也不推荐。

    推荐的方法是添加已经创建/更新的文件的文件路径,像这样[在字符串类型的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);
    
                    }
                });
    

    希望这能解决你的问题。

    【讨论】:

    • 您好,我尝试了您的解决方案,但似乎无法正常工作。由于我添加的 Toast 从未显示过。扫描需要多长时间,因为我在检查之前可能等待的时间不够长。感谢您的帮助!
    • 您能否在控制台上看到带有路径的扫描完整文本?可能需要一段时间。
    • 我仍然无法做到,但我意识到我实际上是将它保存到内部 DCIM 目录中,尽管没有你的帮助它仍然是不可见的。感谢您的帮助!
    【解决方案2】:

    内部存储使用:

    getFilesDir()getCacheDir()

    查看安卓文档:
    https://developer.android.com/training/data-storage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多