java - Android 中的 Imageview 使用从 firebase 检索的 base64 字符串

标签 java android imageview firebase

     int i=0;
  ArrayList<String> list;
        queryRef.addChildEventListener(new ChildEventListener() {

                    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
                        Map<String, Object> value = (Map<String, Object>) snapshot.getValue();
                        String name1 = String.valueOf(value.get("Base64String"));
         **list = new ArrayList<String>();
                        list.add(name1);**
                        System.out.println("Base64 " + i++ +" "+ name1 );
                **System.out.println("1 "+list.toString());**

                    }

此监听器每次获取值并打印,例如:在下面的输出中,我的值会递增,直到从 firebase 获取所有值。

控制台输出:

Base64 0    basfbjksdvkjvskjvbskdj
1           basfbjksdvkjvskjvbskdj
Base64 1    dvfjovbfdjbsklcnsalcks
1           dvfjovbfdjbsklcnsalcks
Base64 2    dvbjsvfbvjksdkvbsdvjkb
1           dvbjsvfbvjksdkvbsdvjkb
Base64 3    qdncwe98yfecbsdjcksbdv
1           qdncwe98yfecbsdjcksbdv

我必须使用这个 base64 字符串并显示图像。下面的代码用于将 Base64 字符串显示为图像(如果存在单个 Base64 字符串)。问题是 String name1 每次检索时都会发生变化,因此必须在放置第一个 base64 字符串后更改 ImageView 。在从 firebase 获取下一个 base64 字符串之前,我不知道如何移动到另一个 ImageView 。

使用 firebase base64 字符串的 imageview 代码

byte[] imageAsBytes = Base64.decode(name1.getBytes(), Base64.DEFAULT); 
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0,imageAsBytes.length) );

如果我使用上面的代码,由于每次名称1中的值发生变化,它不起作用。我怎样才能做到这一点。

编辑:

这是我指的链接 http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

此链接包含

String[] itemname ={
            "Safari",
            "Camera",
            "Global",
            "FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"
        };

    Integer[] imgid={
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
    };

对于上述输入,我必须替换我的输入。但我的图像是 base64 字符串,但示例是使用 Interger 数组。我怎样才能做到这一点。

下面的代码将base64字符串存储在列表中,并将员工姓名存储在列表1中

  name1 = String.valueOf(value.get("Path"));
                    name2 = String.valueOf(value.get("Name"));

                    list.add( name1);
                    list1.add(name2);

下面的代码我将列表值存储在字符串数组中,然后将它们 bundle 在一起发送,以使用输入来显示员工的姓名和图片,而不是itemname和imgid。

        spath = list.toArray(new String[list.size()]);
        sname = list1.toArray(new String[list1.size()]);
        Bundle bundle =new Bundle();
        bundle.putStringArray("names",sname);
        bundle.putStringArray("paths",spath);
        Intent ilist = new Intent(getApplicationContext(),MActivity.class); 
        startActivity(ilist);

提取数据:

String itemname[]=bundle.getStringArray("names");
    String abcd[]=bundle.getStringArray("paths");

最佳答案

试试这个...

在第一个 Activity 中创建 CustomArrayList 而不是 ArrayList 字符串。这样您就不需要将名称保存在另一个数组列表中..

     ArrayList<ListCustomObjects> datas;

在oncreate之后初始化它。

     datas=new ArrayList<ListCustomObjects>();

将此代码放入您的queryRef.addChildEventListener

   ListCustomObjects objects=new ListCustomObjects();
    //you can change to original data
    String name1="Data";
    String name2="Datas";
    objects.setName(name1);
    objects.setPath(name2);
    //add the custom objects to arraylist
    datas.add(objects);

在第一个 Activity 按钮 onclick 中放置此代码...

   Intent secondIntent=new Intent(CustomArrayList.this,SecondArrayList.class);
           Bundle bundle=new Bundle();
           //inorder to send custom arraylist from one activity to another activity we need to use either parcelable or serializable.here am using parcelable for beter performance..
            bundle.putParcelableArrayList("List",datas);
            secondIntent.putExtra("Bundle",bundle);
            startActivity(secondIntent);

在第二个 Activity 中放置此代码...

    ListView listview=(ListView)findViewById(R.id.listView2);
    Bundle bundle=getIntent().getBundleExtra("Bundle");
    ArrayList<ListCustomObjects> datas=bundle.getParcelableArrayList("List");
    SecondArrayListAdapter adapter=new SecondArrayListAdapter(SecondArrayList.this,datas);
    listview.setAdapter(adapter);

并为 ArrayList 自定义对象创建一个类文件作为 ListCustomObjects

public class ListCustomObjects implements Parcelable{


String name;
String path;

public ListCustomObjects() {

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public ListCustomObjects(Parcel in) {
    name = in.readString();
    path= in.readString();

}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(path);
}

public static final Parcelable.Creator<ListCustomObjects> CREATOR = new Parcelable.Creator<ListCustomObjects>() {
    public ListCustomObjects createFromParcel(Parcel in) {
        return new ListCustomObjects(in);
    }

    public ListCustomObjects[] newArray(int size) {
        return new ListCustomObjects[size];

    }
};

}

并将适配器名称创建为SecondArrayListAdapter。这里使用基本适配器。

public class SecondArrayListAdapter extends BaseAdapter {

ArrayList<ListCustomObjects> datas;
Context context;


public SecondArrayListAdapter(Context context,ArrayList<ListCustomObjects> datas) {
this.context=context;
    this.datas=datas;
}

@Override
public int getCount() {
    return datas.size();
}

@Override
public Object getItem(int position) {
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    //where we check the convertview whethere it is creting first time or not
    if(convertView==null){
        convertView=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sample_data_listview_item,null);
        viewHolder=new ViewHolder();

        viewHolder.image=(ImageView)convertView.findViewById(R.id.sample_listview_item_imageView);
        viewHolder.name=(TextView)convertView.findViewById(R.id.sample_listview_item_name);

        convertView.setTag(viewHolder);
    }
    //where we get the views from convertview instead of creating new one.It reduces memory consumption
    else{
        viewHolder=(ViewHolder)convertView.getTag();
    }
    //here we set the name
    viewHolder.name.setText(datas.get(position).getName());
    //here conversion takes place.
    byte[] imageAsBytes = android.util.Base64.decode(datas.get(position).getPath().getBytes(), android.util.Base64.DEFAULT);
    viewHolder.image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    return convertView;
}
//this is used to bind the view as single class
class ViewHolder{
    ImageView image;
    TextView name;
}
}

最终为适配器创建布局作为sample_data_listview_item

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sample_listview_item_imageView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/sample_listview_item_name" />
  </LinearLayout>

关于java - Android 中的 Imageview 使用从 firebase 检索的 base64 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35644367/

相关文章:

java - 使用 RTP 在 JMF 中实现播放器

java - 安卓开发 : Error calling method in MySQLHelper

android - 如何创建此 View (布局)?

Android 重试/取消对话框

javascript - HTML5 - 在 View 播放器中打开 YouTube 视频

android:通过 picasso 从网络加载按钮背景图像

android - 如何从ImageView获取图片并设置到其他ImageView?

java - MySQL结果集到java数组

java - 如何测试具有私有(private)方法、字段或内部类的类?

java - 将新元素放入 arrayList 中,而不是放在其头部