Android 从资产文件夹加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11734803/
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
Load an image from assets folder
提问by kishidp
I am trying to load an image from the asset
folder and then set it to an ImageView
. I know it's much better if I use the R.id.*
for this, but the premise is I don't know the id of the image. Basically, I'm trying to dynamically load the image via its filename.
我正在尝试从asset
文件夹加载图像,然后将其设置为ImageView
. 我知道如果我R.id.*
为此使用 会好得多,但前提是我不知道图像的 id。基本上,我试图通过其文件名动态加载图像。
For example, I randomly retrieve an element in the database
representing let's say a 'cow', now what my application would do is to display an image of a 'cow'via the ImageView
. This is also true for all element in the database
. (The assumption is, for every element there is an equivalent image)
例如,我随机检索的元素database
代表比方说,一个“牛”,现在该怎么办我的应用程序会做的是显示的图像“牛”通过ImageView
。这也适用于database
. (假设是,对于每个元素都有一个等效的图像)
thanks in advance.
提前致谢。
EDIT
编辑
forgot the question, how do I load the image from the asset
folder?
忘了问题,如何从asset
文件夹加载图像?
采纳答案by Erol
If you know the filename in the code, calling this won't be a problem:
如果你知道代码中的文件名,调用这个不会有问题:
ImageView iw= (ImageView)findViewById(R.id.imageView1);
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
Your filename will be the same name as drawableName so you won't have to deal with assets.
您的文件名将与 drawableName 同名,因此您不必处理资产。
回答by Chirag
Checkout this code. IN this tutorial you can find how to load image from asset folder.
签出此代码。在本教程中,您可以找到如何从资产文件夹加载图像。
// load image
//加载图片
try
{
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
回答by osayilgan
Here you are,
这个给你,
public Bitmap getBitmapFromAssets(String fileName) {
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
istr.close();
return bitmap;
}
回答by Nicolas Tyler
Some of these answers may answer the question but I never liked any of them so I ended up writing this, it my help the community.
其中一些答案可能会回答这个问题,但我从不喜欢其中任何一个,所以我最终写了这篇文章,这是我对社区的帮助。
Get Bitmap
from assets:
Bitmap
从资产中获取:
public Bitmap loadBitmapFromAssets(Context context, String path)
{
InputStream stream = null;
try
{
stream = context.getAssets().open(path);
return BitmapFactory.decodeStream(stream);
}
catch (Exception ignored) {} finally
{
try
{
if(stream != null)
{
stream.close();
}
} catch (Exception ignored) {}
}
return null;
}
Get Drawable
from assets:
Drawable
从资产中获取:
public Drawable loadDrawableFromAssets(Context context, String path)
{
InputStream stream = null;
try
{
stream = context.getAssets().open(path);
return Drawable.createFromStream(stream, null);
}
catch (Exception ignored) {} finally
{
try
{
if(stream != null)
{
stream.close();
}
} catch (Exception ignored) {}
}
return null;
}
回答by Prakash Karkee
According to Android Developer Documentationloading with bitmapcan degrade app performane.Here's a link! So doc suggest to use Glidelibrary.
根据Android 开发人员文档加载位图会降低应用程序性能。这是一个链接!所以 doc 建议使用Glide库。
If you want to load image from assets folderthen using Glidelibrary help you alots easier.
如果您想从资产文件夹加载图像,那么使用Glide库可以帮助您更轻松。
just add dependencies to build.gradle (Module:app) from https://github.com/bumptech/glide
只需从https://github.com/bumptech/glide向 build.gradle (Module:app) 添加依赖项
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
sample example :
示例:
// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {
...
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
}
In case not worked by above method : Replace thisobject with viewobject from below code (only if you have Inflate method applied as below in your code).
如果上述方法不起作用:用下面代码中的视图对象替换此对象(仅当您在代码中应用了如下所示的 Inflate 方法时)。
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.book,parent,false);
回答by Yves
This worked in my use case:
这在我的用例中有效:
AssetManager assetManager = getAssets();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
try (
//declaration of inputStream in try-with-resources statement will automatically close inputStream
// ==> no explicit inputStream.close() in additional block finally {...} necessary
InputStream inputStream = assetManager.open("products/product001.jpg")
) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException ex) {
//ignored
}
(see also https://javarevisited.blogspot.com/2014/10/right-way-to-close-inputstream-file-resource-in-java.html)
(另见https://javarevisited.blogspot.com/2014/10/right-way-to-close-inputstream-file-resource-in-java.html)
回答by Anil Singhania
public static Bitmap getImageFromAssetsFile(Context mContext, String fileName) {
Bitmap image = null;
AssetManager am = mContext.getResources().getAssets();
try {
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
回答by Evgeny
WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);