如何在 android:src= 中使用图像 " " 从资产文件夹而不是可绘制对象



当我们在src中使用drawable中的图像时

<ImageView
android:id="@+id/app_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/myImage"/>

我们可以从资产文件夹中使用这里吗

<ImageView
android:id="@+id/app_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="myImage.jpg"/>

不,虽然您可以实际操作,但这不可能直接在XML中实现。

AssetManager assetManager = getAssets();
ImageView imageView = (ImageView) findViewById(R.id.iv_image);
try {
InputStream ims = assetManager.open("my_image.jpg");
Drawable d = Drawable.createFromStream(ims, null);
imageView.setImageDrawable(d);
} catch (IOException ex) {
return;
}

最新更新