开发者

In Android, can I use image from assets in layout xml?

开发者 https://www.devze.com 2023-04-13 09:27 出处:网络
Currently am loading image from drawable resource- <ImageView android:id=\"@+id/stestImg\" android:src=\"@drawable/testImg\"

Currently am loading image from drawable resource-

<ImageView 
       android:id="@+id/stestImg"
       android:src="@drawable/testImg"
       android:lay开发者_如何转开发out_width="wrap_content"
       android:layout_height="wrap_content"
/>

can I use image from assets here, directly to the layout XML? Do I need to do any coding for that! Please help.


you can do it by this way ::

ImageView i;
Bitmap bm = getBitmapFromAsset("pic1.png");
i = (ImageView)findViewById(R.id.image);
i.setImageBitmap(bm);

Call method ::

private Bitmap getBitmapFromAsset(String strName) throws IOException
{
    AssetManager assetManager = getAssets();
    InputStream istr = assetManager.open(strName);
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号