從 JSON 網絡服務檢索 android 中的 byte[] (Retrieve byte[] in android from JSON webservice)


問題描述

從 JSON 網絡服務檢索 android 中的 byte[] (Retrieve byte[] in android from JSON webservice)

I successfully stored image in database  in BLOB format.

Data type for image in webservices model class  is byte [].

Web service:

@POST
@Consumes("application/json")
@Produces("application/json")
@Path("login")
public User Login(User input) {

Request:

[
    {
        "id": 2,
        "password": "******",
        "username": "haris",
        "image": "iVBORw0KGgoAAAANSUhEUgAizDMizDMizDMizDMizDMizDMizDMizDMizDMizDMiz/JPL/AOc68RGNGkMYAAAAAElFTkSuQmCC"
    }
]

We can see that the json service makes conversion to String, byte [] is converted into a String.

I tried to make the conversion from the String to byte [], but for result I can't see any picture data.

byte [] byteData = new byte[imageData().length()];
            
imageView.setImageBitmap(getImage(byteData));


public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

‑‑‑‑‑

參考解法

方法 1:

use like this

byte[] decodedString = Base64.decode(img, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (bitmap != null) {
Drawable image = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 90, 100, true));
}

(by Haris DautovićKapil Vats)

參考文件

  1. Retrieve byte[] in android from JSON webservice (CC BY‑SA 3.0/4.0)

#java #blob #REST #Android #arrays






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論