简体   繁体   English

如何显示来自ArrayList的图像 <Bitmap> (SQLlte)

[英]how to show images from a ArrayList<Bitmap> (SQLlte)

I have this problem where i can not show images (bitmap) from my database in a list on my android studio application. 我有一个问题,我无法在Android Studio应用程序的列表中显示数据库中的图像(位图)。

I can make a picture and save it (base64) in my database and retrieve it. 我可以制作一张图片并将其保存(base64)在我的数据库中并检索它。 But i cannot figure out how to show al the images on a display. 但是我不知道如何在显示器上显示所有图像。

I tried to do it with a list adaptor but that doesnt work :/ 我试图用一个列表适配器来做到这一点,但这不起作用:/

 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);
        }

This is the shortcut way to show multiple view depending on the size of listData . 这是显示多个视图的快捷方式,具体取决于listData的大小。

First add LinearLayout in your_current_activity.xml . 首先在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>

In java class 在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);
      }

Below is my output (Assume I have 5 images in listData) 下面是我的输出(假设我在listData中有5张图像)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM