【问题标题】:Base64 to Bitmap to display in ImageViewBase64 转 Bitmap 在 ImageView 中显示
【发布时间】:2015-09-06 23:09:06
【问题描述】:

我有这段代码可以获取已经使用 web 服务转换为字符串的 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 中?提前致谢。 :)

【问题讨论】:

    标签: android json bitmap


    【解决方案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));
    

    【讨论】:

    • 先生,我尝试了您的代码,但应用程序已停止。这就是我所做的。
    • byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT);位图decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); image.setImageBitmap(Bitmap.createScaledBitmap(decodedByte, image.getWidth(), image.getHeight(), false));
    • mmmm 代码很适合我,使用日志逐步调试代码以了解崩溃发生的时间和地点
    • 先生。非常感谢您。我误解了你的评论。 :) 我已经做到了。 :)
    【解决方案2】:

    也许您的图片缺少高度和宽度?

    尝试使用

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

    您可以使用 720x720 来查看是否是问题所在,如果是,您可以改为传递所需的高度和宽度。

    【讨论】:

    • 谢谢您的回答先生。但是当我在位图解码字节下方添加您的代码时,我更改了 image.setImageBitmap(resizedBitmap)。但是当我运行该应用程序时,它不幸停止了。
    • 我的ImageView的大小和我的图片大小一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2011-07-05
    相关资源
    最近更新 更多