2012-03-30 116 views
-1

我有寫這個代碼,並讀取文件ZIZI.txt如何在沒有root權限的情況下訪問寫入的文件?

//=============== Write To File ZIZI.txt =============================================== 
    private void writeFileToInternalStorage() { 
     String eol = System.getProperty("line.separator"); 
     BufferedWriter writer = null; 
     try { 
      writer = new BufferedWriter(new OutputStreamWriter(openFileOutput(
       "ZIZI.txt", MODE_WORLD_WRITEABLE))); 
      writer.write("This is a test1." + eol); 
      writer.write("This is a test2." + eol); 
     } catch (Exception e) { 
       e.printStackTrace(); 
     } finally { 
      if (writer != null) { 
      try { 
       writer.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
     } 
     Toast.makeText(getBaseContext(),"OK Save", Toast.LENGTH_SHORT).show(); 
    } 

    //================ Read From File ZIZI.txt =========================================== 
    private void readFileFromInternalStorage() { 
     String FF=""; 
     String eol = System.getProperty("line.separator"); 
     BufferedReader input = null; 
     try { 
      input = new BufferedReader(new InputStreamReader(openFileInput("ZIZI.txt"))); 
      String line; 
      StringBuffer buffer = new StringBuffer(); 
      while ((line = input.readLine()) != null) { 
       FF+=line+eol; 
      buffer.append(line + eol); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
     if (input != null) { 
      try { 
      input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
     } 
    Toast.makeText(getBaseContext(),FF, Toast.LENGTH_SHORT).show(); 
    } 

我DDMS該文件中看到:\data\data\setup.myProject\files\ZIZI.txt

但我不能看到這個文件我手機(因爲我沒有root權限)

我想寫我的SD卡或從我的 手機中看到的任何文件夾中讀取。如何更改此代碼?

+0

也許你想要一個應用程序,讓你看到所有文件夾(文件系統),包括根? – 2012-03-30 18:41:18

+0

我只需要知道什麼改變我的代碼,我可以寫入和閱讀文件夾,我可以在手機上看到 – Gold 2012-03-30 19:00:54

回答

0

有你從未使用過Android Developers website?

嘗試開發指南 - >數據存儲 - >Using the External Storage

+0

我討厭這個網站(Android開發者網站)我總是去那裏失去。我只需要知道在我的代碼中要更改什麼,以便我可以在手機上看到並閱讀到文件夾。 – Gold 2012-03-30 19:00:27

0

你需要指定整個路徑到您的文件,即使用getFilesDir內部存儲或getExternalFilesDir()用於外部存儲。例如:

openFileInput(getExternalFilesDir(null) + "/" + "ZIZI.txt"); 
相關問題