2013-02-26 51 views
0

我有一個cheerapp.mp3在我/res/raw文件夾的Android資源文件

所以我的代碼是

String filepath="/res/raw/cheerapp"; //or cheerapp.mp3 
file = new File(filePath); 
FileInputStream in = null; 
try { 
    in = new FileInputStream(file); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

我得到了文件未找到錯誤。爲什麼?

+1

在此處發佈您的錯誤 – 2013-02-26 03:52:18

回答

1

決不可你按路徑訪問的資源文件!

因爲它們是在Android中安裝的APK文件中編譯的。您只能應用內訪問接入資源及其生成的資源ID,從任何活動(上下文):

InputStream cheerSound = this.getResources().openRawResource(R.raw.cheerapp); 

或視圖:

InputStream cheerSound = this.getContext().getResources().openRawResource(R.raw.cheerapp); 

在你的情況,你應該存儲在外部聲音文件SD卡,然後可以通過路徑訪問它們。對於例如,你在sounds文件夾中的文件存儲到SD卡上:

​​

注:路徑以「/」是絕對路徑,因爲「/」代表的類Unix操作系統根(Unix的開始,Linux,Android ...)

3

使用assets文件夾和...

InputStream sound = getAssets().open("filename.mp3"); 

...或raw文件夾和...

InputStream sound = getResources().openRawResource(R.raw.filename); 

,如果它不能幫助你,然後檢查this

0

也許你可以使用AssetManager來管理你的資源。例如:

AssetManager manager = this.getContext().getAssets(); 
InputStream open; 
try { 
    open = manager.open(fileName); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    ... 
} catch (IOException e) { 
    e.printStackTrace(); 
}