从android中的assets文件夹中打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1933015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:50:06  来源:igfitidea点击:

Opening a File from assets folder in android

androidandroid-assets

提问by Mina Samy

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

我在资产文件夹中有一个 .gif 文件,如资产/文件/android.gif。当我尝试打开文件时,它在第二行抛出异常

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.

所以我是否试图打开一个图像文件,尽管如果我尝试打开一个文本文件,同样的代码可以工作?这可能是什么问题。

回答by Tofeeq Ahmad

These Lines are working perfectly--

这些线路运行良好--

InputStream assetInStream=null;

try {
    assetInStream=getAssets().open("icon.png");
    Bitmap bit=BitmapFactory.decodeStream(assetInStream);
    img.setImageBitmap(bit);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(assetInStream!=null)
    assetInStream.close();
}

If your image is very big then you should scale your image before decoding it into Bitmap. See How to display large image efficiently

如果您的图像非常大,那么您应该先缩放图像,然后再将其解码为位图。请参阅如何有效地显示大图像

回答by keno

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.

我怀疑您收到有关未处理的异常类型 IOException 的投诉。如果是这种情况,您需要在 try-catch 块中调用 mgr.open 以处理检索 InputStream 对象时可能发生的异常。

AssetManager mngr = getAssets();
try {
    InputStream is2 = mngr.open("Files/android.gif");
} catch (final IOException e) {
    e.printStackTrace();
}

回答by JRL

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:

不知道事情是否发生了变化,但我在 Android 1.1 中有一个应用程序可以打开图标然后在视图中显示它们,我是这样做的:

BufferedInputStream buf = new BufferedInputStream(mContext.openFileInput(value));
Bitmap bitmap = BitmapFactory.decodeStream(buf);

回答by AdamC

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:

我相信这样做的首选方法是将您的图像放在 res/drawable 目录中。然后你可以得到一个这样的 Drawable:

Drawable d = Resources.getSystem().getDrawable(R.drawable.android);

回答by Someone Somewhere

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 !

Mina,我遇到了同样的问题......我在“资产”中有图像和一个 XML 文件,我可以读取 XML 文件但不能读取图像。经过几个小时的挫折,我终于找到了解决方案!

I have posted the solution here: Null-pointer issue displaying an image from assets folder Android 2.2 SDK

我在这里发布了解决方案: Null-pointer issue shows an image from assets folder Android 2.2 SDK

回答by Adam Freeman

I do not believe gif is supported automatically on Android. Try a png or jpg with the same code.

我不相信 gif 在 Android 上自动支持。尝试使用相同代码的 png 或 jpg。