【问题标题】:How to display image in android如何在android中显示图像
【发布时间】:2016-07-18 06:52:02
【问题描述】:

我想在我的 android 应用程序中显示图像。为此我正在调用我的服务,因为图像采用这种格式:

 image:[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0......].

如何在android中显示此图像。

【问题讨论】:

  • 你的问题不清楚

标签: android wcf


【解决方案1】:

你能解释的更详细一点吗?你的图像是 base 64 格式吗?如果是,那么你必须解码图像并将其显示在 imageview 中。

 byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
        img.setImageBitmap(bitmap);

【讨论】:

    【解决方案2】:

    使用下面的代码

    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    
    image.setImageBitmap(bmp);
    

    这里的 bytearray 是,你从服务器得到什么

    【讨论】:

      【解决方案3】:

      通知您这称为图像的字节数组,您必须使用 BitmapFactory 类的 decodeByteArray 方法将该数组解码为 Bitmap,例如,

      Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
      

      【讨论】:

        【解决方案4】:

        试试这个对我有用

        String myString = "[255,216,255....]";
        try {
            JSONArray arr = new JSONArray(myString);
            byte[] myArray = new byte[myString.length()];
            for (int i = 0; i < arr.length(); i++) {
                myArray[i] = (byte) arr.getInt(i);
            }
            Bitmap bmp = BitmapFactory.decodeByteArray(myArray, 0, myArray.length);
            ImageView image = (ImageView) findViewById(R.id.myImageView);
            image.setImageBitmap(bmp);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        

        解释步骤:

        Start by converting that string to a JsonArray
        Convert all items in it to bytes and add them to an byte[]
        Convert the byte array to a bitmap
        Set that bitmap on your image
        

        【讨论】:

        • 如何使用 json 显示它..like Name,Last Name 我可以使用这个来显示: String Name = json.getString("Stu_FName");并用于显示 myAwesomeTextview.setText("First Name: " + Name ),对于图像我不知道该怎么做。
        • 这里 myString 包含您从 json 获取的数据,上面的代码会将这种格式转换为图像,并且 image.setImageBitmap(bmp);将图像设置到您的图像视图中
        猜你喜欢
        • 2011-06-23
        • 1970-01-01
        • 2015-10-21
        • 2013-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多