2017-08-09 50 views
0

我是Android開發新手。 我確定很多人都有同樣的問題。我有一個銀河S7手機沒有SD卡。所以沒有外部存儲。但我想創建文件與我的應用程序必須從Windows資源管理器訪問導出它。Android Studio - 訪問/存儲/模擬/ 0從Windows資源管理器和Android文件管理器

注:debbug在USB模式 - 無虛擬設備

當然,在我的清單文件我已經設置好的那些2個權限:

android.permission.WRITE_INTERNAL_STORAGE 
android.permission.WRITE_EXTERNAL_STORAGE 

可以肯定我已經創建了一個文件,我用這個寫方法和下面的讀取方法:

File root = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); 
    File customFolder = new File(root.getAbsolutePath() + "/CustomFolder"); 
    customFolder.mkdirs(); 
    file = new File(customFolder, "myData.txt"); 

    // Must catch FileNotFoundException and IOException 
    try { 
     FileOutputStream f = new FileOutputStream(file); 
     PrintWriter pw = new PrintWriter(f); 
     pw.println("Howdy do to you,"); 
     pw.println("and the horse you rode in on."); 
     pw.flush(); 
     pw.close(); 
     f.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "File not found"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "I/O exception"); 
    } 

要讀取myData.txt

try { 
BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     while (true) { 
      line= br.readLine(); 
      if (line== null) break; 
      tv.append("\n" + " " + line); 
     } 
     br.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

所以所有的方法都可以,但是當我瀏覽我的文件系統來獲取創建的文件myData.txt時,我找不到它!

大多數我們安裝在根自己的文件夾中的應用程序,像Snapchat,WhatsApp的的等等等等

我倒要做出同樣的事情,什麼是寫在文件的方式:

ROOT - > MyApplicationName - > CustomFolder

感謝您的幫助:)

回答

0

我終於找到了一個解決方案添加MediaScannerConnection.scanFile()方法。

MediaScannerConnection.scanFile(getApplicationContext(),new String[]{file.getAbsolutePath()},null,new MediaScannerConnection.OnScanCompletedListener() { 
     @Override 
     public void onScanCompleted(String path, Uri uri) { 
      // Do something here if you want 
     }}); 

但這種解決方案並不完全適合我,因爲存儲文件夾:

的Android /數據/ donain_name /文件/文件/ CustomFolder

我想有同樣的結果像whatsapp應用程序可以在內部存儲根目錄中有一個文件夾。

我想要的結果是: 爲MyApplication/CustomeFolder

相關問題