2017-02-17 34 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目錄中,儘管沒有你的幫助,它仍然是不可見的。感謝您的幫助! –