回答

2

您可以將資產文件夾中的文本文件保存到SD卡中的任何位置,然後就可以從其他應用程序讀取文件。

此方法使用getExternalFilesDir,該方法返回主共享/外部存儲設備上應用程序可以放置其擁有的持久性文件的目錄的絕對路徑。這些文件是應用程序的內部文件,對於用戶而言通常不可見爲媒體。

private void copyAssets() { 
AssetManager assetManager = getAssets(); 
String[] files = null; 
try { 
    files = assetManager.list(""); 
} catch (IOException e) { 
    Log.e("tag", "Failed to get asset file list.", e); 
} 
if (files != null) for (String filename : files) { 
    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = assetManager.open(filename); 
     File outFile = new File(Environment.getExternalStorageDirectory(), filename); 
     out = new FileOutputStream(outFile); 
     copyFile(in, out); 
    } catch(IOException e) { 
     Log.e("tag", "Failed to copy asset file: " + filename, e); 
    }  
    finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       // NOOP 
      } 
     } 
     if (out != null) { 
      try { 
       out.close(); 
      } catch (IOException e) { 
       // NOOP 
      } 
     } 
    } 
    } 
} 


private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
} 

,並閱讀:

File dir = Environment.getExternalStorageDirectory(); 
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext"); 
+0

我可以從內部存儲而不是SD卡獲取文件嗎?對不起,我是新的應用程序的Android世界。非常感謝你的幫助:) 加上例如文件是在讓我們說目錄/數據/憤怒的鳥(我只是舉例,我可以訪問其他文件數據),那麼我會怎麼去做這件事。請給你的代碼解釋一下。再次感謝:) – Abdulrehman

+0

我編輯和解釋更好:) – Carlos

+1

謝謝,如果我卡住了一些地方,我會再次打擾你:)。非常感謝,非常抱歉,但我是一名新程序員 – Abdulrehman

3

這是可能的使用標準Android的存儲,所有用戶的文件存儲也:

所有你需要做的是訪問相同的文件和相同的路徑,例如:

String fileName = Environment.getExternalStorageDirectory().getPath() + "myFolderForBothApplications/myFileNameForBothApplications.txt"; 

其中myFolderForBothApplicationsmyFileNameForBothApplications可以通過文件夾/文件名來代替,但是這需要在這兩個應用程序相同的名稱。

Environment.getExternalStorageDirectory()返回一個類文件的對象到共用,可使用的設備的文件目錄中,相同的文件夾中的用戶也可以看到。 通過調用getPath()方法,返回表示該存儲路徑的字符串,以便隨後添加文件夾/文件名。

因此,一個完整的代碼示例:

String path = Environment.getExternalStorageDirectory().getPath() + "myFolderForBothApplications/"; 
String pathWithFile = path + "myFileNameForBothApplications.txt"; 

File dir = new File(path); 
if(!dir.exists()) {  //If the directory is not created yet 
    if(!dir.mkdirs()) { //try to create the directories to the given path, the method returns false if the directories could not be created 
     //Make some error-output here 
     return; 
    } 
} 
File file = new File(pathWithFile); 
try { 
    f.createNewFile(); 
} catch (IOException e) { 
    e.printStackTrace(); 
    //File couldn't be created 
    return; 
} 

之後,你可以在文件中寫入或讀取文件作爲例如提供在this answer

請注意,像這樣存儲的文件是對用戶可見,並且我可以由用戶編輯/刪除。

還要注意什麼的getExternalStorageDirectory()的JavaDoc說:

返回的主要外部存儲目錄。如果用戶在他們的計算機上安裝了該目錄,該設備目前可能無法訪問,該設備已從設備中刪除,或發生其他問題。您可以使用getExternalStorageState()來確定其當前狀態。

我不知道這是否是解決您的問題的最佳/最安全的方式,但它應該起作用。

相關問題