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

I am currently loading an image from a resource that can be rendered -

<ImageView android:id="@+id/stestImg" android:src="@drawable/testImg" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Is it possible to use the image from the assets here directly in the XML layout? Do I need to code? Please help.

+8
android android-layout android-imageview
source share
1 answer

you can do it 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; } 
+20
source share

All Articles