doumei1908 2018-02-13 15:33
浏览 206
已采纳

从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 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());

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥115 用Java解决探地雷达dzt文件的解析过程
  • ¥20 有关神经网络这道(b)determine the parameters of neural network
  • ¥25 annaconda jvpyter
  • ¥20 来一个会抓包app支付接口的
  • ¥30 vivado2017.4的sdk打不开
  • ¥50 matlab,文献复现
  • ¥20 寻找Flink CDC指导,有偿咨询
  • ¥15 cruise如何仿真不同温度下车辆的经济性?
  • ¥20 扑克牌游戏的完整代码
  • ¥88 求帮安装Python架构的vnpy