Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android csv文件在写入SD卡时不可见_Java_Android_Csv_Android External Storage_File Writing - Fatal编程技术网

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

Java Android csv文件在写入SD卡时不可见,java,android,csv,android-external-storage,file-writing,Java,Android,Csv,Android External Storage,File Writing,我遇到了一个网站上其他人已经遇到的问题。我尝试过这些解决方案,但它们对我不起作用 我目前正在开发一个应用程序,可以根据用户输入创建csv文件。当设备通过USB连接到PC时,用户需要能够检索该文件。保存代码功能正常工作,但设备连接后,文件在PC上不可见 这是应用程序的完整保存代码段: FileWriter fileWriter = null; File sdCard = Environment.getExternalStoragePublicDirectory(Environmen

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

我目前正在开发一个应用程序,可以根据用户输入创建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());
    }
文件正在写入外部存储器,虽然我可以看到占用了更多的空间,但文件本身仍然不可见


如果您能帮助解决问题,我们将不胜感激。谢谢大家!

您向MediaScanner发送广播的方式,但可能无法有效工作,因此不推荐使用这种方式

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

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

希望这能解决您的问题。

对于内部存储使用:

getFilesDir()
getCacheDir()

请参阅android文档:

您好,我尝试了您的解决方案,但似乎效果不太好。我加的祝酒词从来没有展示过。扫描需要多长时间,因为我可能没有等足够长的时间来检查。谢谢你的帮助!您是否能够在控制台上看到带有路径的扫描完整文本?这可能需要一段时间。我仍然不能,但我意识到我实际上是在将它保存到内部DCIM目录,尽管没有您的帮助,它仍然是不可见的。谢谢你的帮助!
            MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() {

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

                }
            });