【发布时间】:2019-08-27 21:30:33
【问题描述】:
我正在尝试将位图加载到ImageView。
如果我这样做:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
imageView.setImageBitmap(bitmap);
什么都不显示。
如果我这样做:
android:src="@drawable/myimage"
显示图像。
我的活动extends AppCompatActivity 和我的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
有什么想法吗?
【问题讨论】:
-
myimage 是什么类型的可绘制资源?
-
@smitty1:实际上我刚刚看到
BitmapFactory.decodeResource(getResources(), R.drawable.myimage)返回null。我什至尝试了android的drawable(例如ic_launcher_foreground)都不起作用(也返回null) -
@smitty1:
myimage是矢量可绘制的 -
如果尝试将矢量转换为位图,这将不起作用。你需要使用画布。
-
@Rajnishsuryavanshi:我该怎么做?
标签: android android-activity android-imageview android-image