14

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.

codingB2
  • 153
  • 1
  • 1
  • 8

1 Answers1

23

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;
 }
Uttam
  • 12,361
  • 3
  • 33
  • 30
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • i have around 150 images and i need to load all images from assets folder so how can i load all images from assets can someone pls help me – user3233280 Feb 01 '14 at 17:16
  • @user3233280 why do you required to load its from assets instread of Sdcard is also option – Nikunj Patel Feb 03 '14 at 13:56