doumei1908
2018-02-13 15:33 阅读 147

从HTML读取Base64图像到android picasso适配器

I use PHP with upload64 to upload images which I then store in MYSQL database.

The AJAX part runs fine and image appears.
Now I want to display my picture on Android using the base64 image data data:image/jpeg;base64,/9j/4gIcSUNDX1BST0ZJTEUAAQ...

Here is my attempt to read it in Android using "recycler" view adapter (using picasso library and Android studio)

JSONObject jsonObject = new JSONObject(response);
JSONObject businessObject = jsonObject.getJSONObject("result");
JSONArray array = businessObject.getJSONArray("data");

Log.d("TAG DATA 123", String.valueOf(array));

for (int i = 0; i < array.length(); i++) {
  JSONObject o = array.getJSONObject(i);
  String base64Content = jsonObject.getString(o.getString("picture"));
  byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
  Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

  ModelsKejadian item = new ModelsKejadian(
    o.getString("judul_laporan"),
    o.getString("id_user_pelapor"),
    o.getString("id_sys_tragedy_child"),
    o.getString("proses"),
    o.getString("datetime"),
    o.getString("ket_tambahan"),
    bitmap
  );
  modelkejadianlist.add(item);
}

adapter = new AdapterKejadian(getActivity(), modelkejadianlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

But this fails at loading the image.

public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        ModelsKejadian modelKejadian = modelsKejadianList.get(position);
        holder.nama_profile.setText(modelKejadian.getNama_profile());
        holder.kategori_kejadian.setText(modelKejadian.getKategori_Kejadian());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if(Objects.equals(modelKejadian.getStatus_proses(), "PROSES")){
                holder.status_proses.setTextColor(Color.parseColor("#d8df3734"));
            }else if(Objects.equals(modelKejadian.getStatus_proses(), "SELESAI")){
                holder.status_proses.setTextColor(Color.parseColor(String.valueOf("#17531e")));
            }
        }
        holder.status_proses.setText(modelKejadian.getStatus_proses());
        holder.waktu.setText(modelKejadian.getWaktu());
        holder.judul_kejadian.setText(modelKejadian.getJudul_kejadian());
        holder.isi_laporan.setText(modelKejadian.getIsi_laporan());
        Picasso.with(mContext).load(modelKejadian.getGambar_laporan()).fit().into(holder.gambar_kejadian);

        holder.optionMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "Item is clicked" +  position, Toast.LENGTH_SHORT).show();
            }
        });



    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dtypj3308 dtypj3308 2018-02-13 16:23

    Remove data:image/jpeg;base64, from the string before using Base64.decode.

    Then in your adapter, holder.gambar_kejadian.setImageBitmap(modelKejadian.getGambar_laporan());

    点赞 评论 复制链接分享

相关推荐