从android中的assets文件夹中打开文件
我的 asset 文件夹中有一个 .gif 文件,如 asset/Files/android.gif。当我尝试打开文件时,它在第二行抛出异常,
AssetManager mngr=getAssets();
InputStream is2=mngr.open("Files/android.gif");
所以我是否正在尝试打开图像文件,尽管如果我尝试打开文本文件,相同的代码可以工作? 这里可能有什么问题。
I have a .gif file inside the assets folder like this assets/Files/android.gif. when I try to open the file it throws an exception at the second line
AssetManager mngr=getAssets();
InputStream is2=mngr.open("Files/android.gif");
so Is it that I'm trying to open an image file despite that the same code works if I try to open a text file ?
what can be the problem here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这些线工作得很好——
如果您的图像非常大,那么您应该在将图像解码为位图之前缩放图像。 查看如何高效显示大图像
These Lines are working perfectly--
If your image is very big then you should scale your image before decoding it into Bitmap. See How to display large image efficiently
我怀疑您收到有关未处理的异常类型 IOException 的投诉。如果是这种情况,您需要将对 mgr.open 的调用放在 try-catch 块中,以处理检索 InputStream 对象时可能发生的异常。
I suspect you are getting complaints about unhandled exception type IOException. If that's the case, you need to put the call to mgr.open in a try-catch block to handle the exception that may occur when retrieving the InputStream object.
不知道事情是否发生了变化,但我在 Android 1.1 中有一个应用程序,它打开图标,然后将它们显示在视图中,我这样做了:
Don't know if things have changed or not but I had an app in Android 1.1 that opened icons to then display them in a view and I did it like so:
我相信最好的方法是将图像放在 res/drawable 目录中。然后你可以得到一个像这样的Drawable:
I believe the preferred way to do this is to put your image in the res/drawable directory. Then you can get a Drawable like this:
Mina,我遇到了同样的问题...我在“资产”中有图像和 XML 文件,我可以读取 XML 文件,但不能读取图像。经过几个小时的挫折,我终于找到了解决方案!
我已经在这里发布了解决方案:
空-显示资源文件夹 Android 2.2 SDK 中的图像的指针问题
Mina, I had the same problem... I had images and an XML file within "assets" and I could read the XML file but not the images. After a couple of hours of frustration I finally found the solution !
I have posted the solution here:
Null-pointer issue displaying an image from assets folder Android 2.2 SDK
我不相信 Android 会自动支持 gif。尝试使用相同代码的 png 或 jpg。
I do not believe gif is supported automatically on Android. Try a png or jpg with the same code.