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

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

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

报告相同问题?

悬赏问题

  • ¥30 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52