2012-06-08 28 views
0

我用下面的方法來如何使用其他Android應用

private void writeFileToInternalStorage() { 

    String eol = System.getProperty("line.separator"); 
    BufferedWriter writer = null; 

try{ 
    writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myFile.txt", MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE))); 
     writer.write("Hello world!"); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (writer != null) 
     { 
     try 
     { 
      writer.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     } 
    } 
} 

然後我試圖從另一個Android讀取文件中的一個Android應用程序的數據寫入一個文件來讀取一個Android應用程序寫入到文件數據使用此方法的應用程序

private void readFileFromInternalStorage(){ 
    String eol = System.getProperty("line.separator"); 
    BufferedReader input = null; 
    try 
    { 
     input = new BufferedReader(new InputStreamReader(openFileInput("myFile1.txt"))); 
     String line; 
     StringBuffer buffer = new StringBuffer(); 
     while ((line = input.readLine()) != null) 
     { 
      buffer.append(line + eol); 


     } 

     TextView tv = (TextView) findViewById(R.id.textView); 
     tv.setText(buffer.toString().trim()); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (input != null) 
     { 
      try 
      { 
       input.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

第二種方法無法讀取文件。我還添加了讀寫權限,但它只顯示空白屏幕。什麼可以是錯誤,我該如何糾正?我是Android新手,需要你的幫助。

謝謝!

+0

如果兩個方法都在一個應用程序中,這兩個方法正常工作。 – Grant

回答

1

問題是

openFileOutput("myFile.txt", MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE)) 

文檔說:

此文件寫入路徑相對於到您的應用程序內

所以的情況下,你在相對路徑寫入文件,程序1和試圖從相對路徑 閱讀應用程序2.

你應該能夠調用Environment.getExternalStorageDirectory()來獲取根路徑到SD卡並使用它來創建FileOutputStream。從那裏,只需使用標準的java.io例程。

查看下面的代碼片段將文件寫入SD卡。

private void writeToSDCard() { 


try 
     { 
      File file = new File(Environment.getExternalStorageDirectory(), 
        "filename"); 
      BufferedWriter writer = new BufferedWriter(new FileWriter(file)); 
      writer.write("Hello World"); 
      writer.close(); 
     } catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 

看下面片段來讀取保存在SD卡上的文件。

private void readFileFromSDCard() { 
    File directory = Environment.getExternalStorageDirectory(); 
    // Assumes that a file article.rss is available on the SD card 
    File file = new File(directory + "/article.rss"); 
    if (!file.exists()) { 
     throw new RuntimeException("File not found"); 
    } 
    Log.e("Testing", "Starting to read"); 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new FileReader(file)); 
     StringBuilder builder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      builder.append(line); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

在哪裏添加?我在Android編程方面沒有這方面的知識。我只是一個初學者:) – Grant

+0

在應用程序1中調用寫函數onClick的按鈕,並在應用程序2中調用讀函數onClick的按鈕。 –

+0

包括在應用程序清單1 –

0

最好的辦法是把它放到scdcard成類似/sdcard/Android/data/package/shared/