Android Asset Loading

2019-08-28 20:59发布

问题:

Still haven't found an answer to this question. I've been copying and pasting source code from a book, and this is one of the example activities. Unfortunately when I run the activity it goes straight to the "catch" meaning that the "try" failed. When debugging, it seems like I get a FileNotFound exception at "AssetFileDescriptor descriptor = assetManager.openFd("bloop.wav")". I put the file in the project's assets folder just like I do with any other file (right click assets>new>file>select file to upload). I see the file in the eclipse hierarchy inside assets as I should. Is there something else I'm not doing? do I need to declare the asset in the manifest file or something? Any help is greatly appreciated. thanks

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    setContentView(textView);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = new MediaPlayer();
    try {
        AssetManager assetManager = getAssets();
        AssetFileDescriptor descriptor = assetManager.openFd("bloop.wav");//This is the exception
        mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
        mediaPlayer.prepare();
        mediaPlayer.setLooping(true);
    } catch (IOException e) {
        Log.d("TAG", "Error was this  ", e);
        mediaPlayer = null;
    }
}

And here is my error, for those of you that will want the full logcat:

Error was this  
java.io.FileNotFoundException: bloop.wav
at android.content.res.AssetManager.openAssetFd(Native Method)
at android.content.res.AssetManager.openFd(AssetManager.java:332)
at com.example.secondproject.MediaPlayerTest.onCreate(MediaPlayerTest.java:24)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

回答1:

Please check that Eclipse actually added your file to the project and that the file is named "bloop.wav". Go into the project folder using your file explorer/finder/etc and see if it's there.

Also look into the .apk that Eclipse creates (should be in the /bin folder of you project).

Change the file extension to .zip and have a look inside. If the wav is not there it might be that the /assets folder is not added to the build path (in Eclipse go to File > Properties > Java Build Path > Source (Tab) and maybe do a "Link Source" on the /assets folder if it's not there)

Edit:

What is the output of the following code?

public void onCreate(Bundle savedInstanceState) {
    /.../
    try {
        AssetManager assetManager = getAssets();
        String[] list = assetManager.list("");
        for (String s : list) {
            System.out.println(s);
        }
    } catch (IOException e) {
        Log.d("TAG", "Error was this  ", e);
    }
}


回答2:

Finally!! I've discovered the problem. Thank you very much @Hawkeye for giving your help, but it turns out the problem was with the way the files were placed in the assets folder. Normally I go to file > new > file > advanced (link from disk) in order to place files in my workspace, as I do with my other java programs. for SOME REASON this is not recognized for android applications and I had to go to the file on my computer, hit "copy", then go into my workspace and "paste" a physical copy of the file in the assets folder. For some reason, uploading the files in this manor causes the "assets" FOLDER to be created in the .apk file (if you do the link method, the file will be in the .apk, but the assets folder will not be there!). This solution is not posted anywhere else online and I had to discover this on my own. I hope this is able to help someone else.