从“资产”文件夹加载映像

时间:2022-05-04 00:26:29

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)

例如,我在数据库中随机检索一个表示“cow”的元素,现在我的应用程序将通过ImageView显示“cow”的图像。这对于数据库中的所有元素也是如此。(假设每个元素都有一个等效的图像)

thanks in advance.

提前谢谢。

EDIT

编辑

forgot the question, how do I load the image from the asset folder?

忘记了问题,如何从asset文件夹加载映像?

6 个解决方案

#1


28  

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相同,因此您不必处理资产。

#2


99  

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;
}

#3


34  

Here you are,

给你,

  public Bitmap getBitmapFromAssets(String fileName) {
    AssetManager assetManager = getAssets();

    InputStream istr = assetManager.open(fileName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    return bitmap;
}

#4


4  

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:

从资产获得位图:

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:

得到可拉的资产:

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;
}

#5


3  

WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);

#6


0  

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;
    }

#1


28  

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相同,因此您不必处理资产。

#2


99  

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;
}

#3


34  

Here you are,

给你,

  public Bitmap getBitmapFromAssets(String fileName) {
    AssetManager assetManager = getAssets();

    InputStream istr = assetManager.open(fileName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    return bitmap;
}

#4


4  

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:

从资产获得位图:

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:

得到可拉的资产:

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;
}

#5


3  

WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("file:///android_asset/pract_recommend_section1_pic2.png");
web.getSettings().setBuiltInZoomControls(true);

#6


0  

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;
    }