java - 如何显示 ArrayList<Bitmap> 中的图像 (SQLlte)

标签 java android sqlite android-sqlite android-bitmap

我遇到这个问题,无法在 Android Studio 应用程序的列表中显示数据库中的图像(位图)。

我可以制作一张图片并将其保存(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的大小显示多个 View 的快捷方式。

首先在 your_current_activity.xml 中添加 LinearLayout

<?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 个图像)

enter image description here

关于java - 如何显示 ArrayList<Bitmap> 中的图像 (SQLlte),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55666389/

相关文章:

android - 完成其父类(super class)的子类 Activity

android - 将相机移动到 map View 上的某个位置

android - 为什么堆栈跟踪行号与源代码行号不匹配?

c# - 在 C# 中,有什么方法可以将内存中的文件作为内存中的 SQLite 数据库与 System.Data.SQLite 链接?

python - 接线员不在 Peewee

java - 使用三元运算符时出错

java - 为什么我们可以将 FutureTask<V> 对象分配给 Future<V> 变量?

java - 无法在 Kotlin 中使用 Apache Commons-IO

android - 立即在 Android 上显示自动完成

java - Spring Data 存储库实际上是如何实现的?