Base64到Bitmap以在ImageView中显示

时间:2015-06-21 11:30:40

标签: android json bitmap

我有这个代码,它使用webservice获取已经转换为字符串的BLOB图像。这是JSON输出。

{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并在布局中显示它。除图像外,其他值均已显示。

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

我的代码有什么问题,为什么图像不能在ImageView中显示?提前致谢。 :)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

也许你的图像缺少高度和宽度?

尝试使用

// resize bitmap
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 720, 720, true);

你可以使用720x720来查看是否存在问题,如果是,你可以通过所需的高度&amp;相反宽度。