2014-04-03 36 views
0

我想在Android中創建應用程序。我想要訪問放置在res/raw文件夾中的文件,如何通過File()構造函數訪問它時如何指定其路徑?我怎樣才能使用textview顯示文件的內容?在Android中訪問res /原始文件夾

+2

你不能用File()構造函數訪問它,因爲它實際上不是一個文件,而是一個android-unique結構。請參閱http://developer.android.com/guide/topics/resources/accessing-resources.html文檔 –

回答

0

使用InputStream讀取文件內容。

getResources().getIdentifier("raw/FILENAME_WITHOUT_EXTENSION", 
          "raw", getPackageName()); 

爲了得到它作爲一個的InputStream

InputStream ins = getResources().openRawResource(
      getResources().getIdentifier("raw/FILENAME_WITHOUT_EXTENSION", 
      "raw", getPackageName())); 

OR

InputStream inputStream = getResources().openRawResource(R.raw.yourresource); 

參考http://developer.android.com/guide/topics/resources/providing-resources.html提供資源。

+0

是啊!我得到它通過使用InputStream inputStream = getResources()。openRawResource(R.raw.yourresource);謝謝:) – user3494875

0

看起來像這樣

InputStream raw = context.getAssets().open("filename.ext"); 

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8")); 

從這裏取:Android how to get access to raw resources that i put in res folder?

另一種方式我想你可以做到這一點是使用getResources(),並獲得它通過R.raw.fileName

+0

是啊!我得到它通過使用InputStream inputStream = getResources()。openRawResource(R.raw.yourresource);謝謝:) – user3494875

相關問題