2017-02-17 65 views
0

我一直有一个问题,该网站上的其他人已经。我已经尝试过这些解决方案,但他们不适合我。写入SD卡时Android csv文件不可见

我目前正在开发一个应用程序,它会根据用户输入创建一个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()); 
    } 

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

任何帮助什么可能是问题将不胜感激。谢谢!

回答

0

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

推荐的方法是添加更新,这样[中String类型的ArrayList()已创建的文件/

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

现在你需要运行SCANFILE的静态方法的文件路径MediaScannerConnection class and pass字符串数组,包含已创建/更新且需要进行介质扫描的所有文件列表

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

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

       } 
      }); 

希望这可以解决您的问题。

+0

嗨,我试过你的解决方案,它似乎并没有正常工作。因为我添加的Toast从未显示过。 扫描需要多长时间,因为在检查之前我可能没有等待足够长的时间。 谢谢你的帮助! –

+0

您是否能够在控制台上看到带有路径的扫描完整文本?这可能需要一段时间。 – MobileEvangelist

+0

我还是没有办法,但我意识到我实际上是将它保存在内部的DCIM目录中,尽管没有你的帮助,它仍然是不可见的。感谢您的帮助! –