从assets文件夹加载图像

时间:2012-07-31 07:01:25

标签: android image assets

我正在尝试从asset文件夹加载图片,然后将其设置为ImageView。我知道如果我使用R.id.*这样做会好得多,但前提是我不知道图像的id。基本上,我试图通过其文件名动态加载图像。

例如,我随机检索database中的一个元素,代表一个'cow',现在我的应用程序将显示一个'的图像通过ImageView牛'。对于database中的所有元素也是如此。 (假设是,每个元素都有一个等价的图像)

提前感谢。

修改

忘记了这个问题,如何从asset文件夹加载图片?

8 个答案:

答案 0 :(得分:107)

结帐code。在本教程中,您可以找到如何从资产文件夹加载图像。

//加载图片

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

答案 1 :(得分:43)

你在这里,

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

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

    return bitmap;
}

答案 2 :(得分:32)

如果你知道代码中的文件名,那么调用它不会有问题:

ImageView iw= (ImageView)findViewById(R.id.imageView1);  
int resID = getResources().getIdentifier(drawableName, "drawable",  getPackageName());
iw.setImageResource(resID);

您的文件名与drawableName的名称相同,因此您无需处理资产。

答案 3 :(得分:6)

其中一些答案可能会回答这个问题,但我从来没有喜欢过这些问题,所以我最后写了这篇文章,这是我对社区的帮助。

从资产中获取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;
}

从资产中获取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;
}

答案 4 :(得分:2)

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

答案 5 :(得分:1)

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

答案 6 :(得分:1)

根据 Android开发者文档,使用位图加载会降低应用程序的性能。这里是a link!因此,文档建议使用 Glide 库。

如果您要从资产文件夹中加载图像,则使用 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'
}

示例示例:

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

如果上述方法无法使用:用下面代码中的 view 对象替换 this 对象(仅当您在代码中应用了如下Inflate方法时)。< / p>

 LayoutInflater mInflater =  LayoutInflater.from(mContext);
        view  = mInflater.inflate(R.layout.book,parent,false);

答案 7 :(得分:1)

这在我的用例中有效:

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
}

(另请参见https://javarevisited.blogspot.com/2014/10/right-way-to-close-inputstream-file-resource-in-java.html

相关问题