Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Currently am loading image from drawable resource-

<ImageView 
       android:id="@+id/stestImg"
       android:src="@drawable/testImg"
       android:layout_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.

like image 228
codingB2 Avatar asked Oct 15 '11 07:10

codingB2


1 Answers

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;
 }
like image 193
Nikunj Patel Avatar answered Oct 17 '22 14:10

Nikunj Patel