我有这个问题,我无法在我的android工作室应用程序的列表中显示我的数据库中的图像(位图)。
我可以制作一张图片并将其保存(我的数据库中的base64)并检索它。但我无法弄清楚如何在显示器上显示图像。
我尝试使用列表适配器,但这不起作用:/
ArrayList<Bitmap> listData = new ArrayList<>();
while(data.moveToNext()){
//get the value from the database in column 1
//then add it to the ArrayList
byte [] encodeByte =Base64.decode(data.getString(1),Base64.DEFAULT);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap =BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
listData.add(bitmap);
}
这是根据listData
的大小显示多个视图的快捷方式。
首先在LinearLayout
中添加your_current_activity.xml
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
</LinearLayout>
在java类中
LinearLayout linearLayout = findViewById(R.id.linearLayout);
ArrayList<Bitmap> listData = new ArrayList<>();
for (Bitmap a : listData) {
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
image.setMaxHeight(20);
image.setMaxWidth(20);
image.setImageBitmap(a);
linearLayout.addView(image);
}
下面是我的输出(假设我在listData中有5个图像)