Base64编码为位图中的ImageView显示 [英] Base64 to Bitmap to display in ImageView

查看:153
本文介绍了Base64编码为位图中的ImageView显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code这让正在使用Web服务已经转换成字符串BLOB形象。这是JSON输出。

I have this code which get BLOB image that is already converted to string using webservice. This is the JSON output.

{driver_name: "Anna Biendia"
 taxi_plate_no: "NUV 900"
 driver_contact_no: "09169271825"
 driver_operator: "grab"
 driver_operator_address: "987 Buendia St. California"
 image: "iVBORw0KGgoAAAANSUhEUgAACDQAAAXcCAYAAADXlEzmAAAACXBIWX..."}

这是android系统当中去了JSON和布局显示它在我的code。已显示,除了在图像的其它值。

This is my code in android which get the JSON and display it in the layout. The other values have been displayed except for the image.

public class DriverDetails extends Activity {
ArrayList<Objects> objectsList = new ArrayList<>();
String url = "http://192.168.1.110:8080/taxisafe3/displays/taxidetails";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_details);

    new Task().execute(url);
}
public class Task extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {
        String content = HttpULRConnect.getData(url);
        return content;
    }

    @Override
    protected void onPostExecute(String s) {
        try {
            TextView title1 = (TextView) findViewById(R.id.textView3);
            TextView title = (TextView) findViewById(R.id.textView2);
            TextView title2 = (TextView) findViewById(R.id.textView7);
            TextView title3 = (TextView) findViewById(R.id.textView9);
            TextView title4 = (TextView) findViewById(R.id.textView11);
            ImageView image = (ImageView) findViewById(R.id.imageView2);

            JSONArray ary = new JSONArray(s);
            for (int i = 0; i < ary.length(); i++) {
                JSONObject jsonobject = ary.getJSONObject(i);
                Objects objects = new Objects();
                objects.setDriver_name(jsonobject.getString("driver_name"));
                objects.setTaxi_plate_no(jsonobject.getString("taxi_plate_no"));
                objects.setDriver_operator(jsonobject.getString("driver_operator"));
                objects.setDriver_operator_address(jsonobject.getString("driver_operator_address"));
                objects.setDriver_contact_no(jsonobject.getString("driver_contact_no"));
                objects.setImage(jsonobject.getString("image"));
                objectsList.add(objects);


                if (title1 != null){
                    title1.setText(objects.getDriver_name());
                }
                if (title != null){
                    title.setText(objects.getTaxi_plate_no());
                }
                if (title2 != null){
                    title2.setText(objects.getDriver_operator());
                }
                if (title3 != null){
                    title3.setText(objects.getDriver_operator_address());
                }
                if (title4 != null){
                    title4.setText(objects.getDriver_contact_no());
                }
                if(image != null){
                    byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                    image.setImageBitmap(decodedByte);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
}
}
}

什么是错误的,我code为什么图像不显示ImageView的?提前致谢。 :)

What is wrong in my code why the image doesn't display in ImageView? Thanks in advance. :)

推荐答案

转换的base64 之后的位图

byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

调整你的位图ImageView的高度和宽度

image.setImageBitmap(Bitmap.createScaledBitmap(decodedByte, image.getWidth(), image.getHeight(), false));

这篇关于Base64编码为位图中的ImageView显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆