0

フォルダcheerapp.mp3にがあります/res/raw

だから私のコードは

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();
}

ファイルが見つからないというエラーが発生しました。なぜ?

4

3 に答える 3

3

フォルダを使用assetsして...

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

... またはrawフォルダと ...

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

役に立たない場合は、これを確認してください

于 2013-02-26T03:57:20.240 に答える
1

パスでリソースファイルにアクセスすることはできません。

AndroidにインストールされているAPKファイルにコンパイルされているためです。アプリケーション内のリソースにアクセスするには、任意のアクティビティ(コンテキスト)から、生成されたリソースIDにアクセスする必要があります。

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

またはビューから:

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

あなたの場合、サウンドファイルを外部SDカードに保存してから、パスでそれらにアクセスできるようにする必要があります。たとえば、ファイルをsoundsSDカードのフォルダに保存します。

FileInputStream inFile = new FileInputStream("/mnt/sdcard/sounds/cheerapp.mp3");

注:パスは「/」で始まります。「/」はUnixライクなOS(Unix、Linux、Androidなど)のルートを表すため、絶対パスです。

于 2013-02-26T04:26:51.327 に答える
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();
}
于 2013-02-26T03:58:42.913 に答える