2013-08-30 43 views
0

我在我的項目中的以下三行代碼:爲什麼我無法在我的android項目中獲取資源文件?

String filePathString = "android.resource://"+this.getPackageName()+"/res/raw/oldphone_mono.wav"; 
File f = new File(filePathString); 
if(f.exists()) {/* do something */} 

在運行時,filePathString的值爲android.resource://com.johntestapp/res/raw/oldphone_mono.wav。在我的項目中,我可以看到我的/ res/raw /目錄中有文件oldphone_mono.wav。但由於某種原因,f.exists()仍然評估爲false。我聲明我的wav文件的路徑有什麼問題?

+3

嘗試'myActivity.getResources()。openRawResource(R.raw.oldphone_mono)' –

回答

2

InputStream inputStream = getResources().openRawResource(R.raw.oldphone_mono);來代替。

+0

但我想最終做一個'MediaPlayer p = new MediaPlayer(); p.setDataSource(this,Uri.parse(filePathString));'。有沒有辦法將inputStream轉換爲filePathString? – John

+0

在這種情況下,你想要的是'MediaPlayer mp = MediaPlayer.create(this,R.raw.oldphone_mono);' – AxiomaticNexus

相關問題