Android资源加载 [英] Android Asset Loading

查看:220
本文介绍了Android资源加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然没有找到这个问题的答案。我一直在从一本书中复制和粘贴源代码,这是示例活动之一。不幸的是,当我运行该活动时,它会直接转到捕获,这意味着尝试失败了。调试时,似乎在 AssetFileDescriptor描述符= assetManager.openFd( bloop.wav)处出现FileNotFound异常。就像处理其他文件一样,我将文件放在项目的资产文件夹中(右键单击资产>新建>文件>选择要上传的文件)。我应该在资产内部的Eclipse层次结构中看到该文件。我还有别的吗?我需要在清单文件中声明资产吗?任何帮助是极大的赞赏。谢谢

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)


推荐答案

最后!我发现了问题。非常感谢@Hawkeye提供的帮助,但事实证明问题出在文件在资产文件夹中的放置方式。通常,我会转到文件>新建>文件>高级(来自磁盘的链接),以便像在其他Java程序中一样将文件放置在工作区中。对于某些原因,Android应用程序无法识别该错误,因此我不得不转到计算机上的文件,单击复制,然后进入我的工作区,然后将物理文件的粘贴副本粘贴到Assets文件夹中。出于某种原因,在此庄园中上载文件会导致在.apk文件中创建资产文件夹(如果执行链接方法,该文件将位于.apk中,但资产文件夹将不存在! )。此解决方案未发布在其他任何在线位置,我不得不自己发现它。我希望这可以帮助其他人。

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.

这篇关于Android资源加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆