0

我已经开发了以下代码来将 base64 字符串数据转换为字节数组,但它正在显示IOException

public static EncodedImage getProfileByteArray(String profilePic){
    byte[] data=profilePic.getBytes();
    byte[] base64Data;
    Bitmap bitmap=null;
    EncodedImage fullImage=null;
    try {
        base64Data = Base64InputStream.decode(data, 0, data.length);
        fullImage = EncodedImage.createEncodedImage(base64Data, 0, base64Data.length);
        bitmap = fullImage.getBitmap();
        System.out.println(data);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println(e+"lkllllllllllllllll");
    }
    catch(Exception e){
        System.out.println(e+"lkllllllllllllllll");
    }
    return fullImage;
}

方法是我正在调用一个 web 服务,通过它我获取 base64 图像字符串并尝试将此 base64 字符串转换为字节数组以显示EncodedImage格式的图像。

4

1 回答 1

0

您是否尝试显示从 web 服务获取的 url 中的图像?url 是从 web 服务返回的图像吗?如果是这样,请尝试以下代码并告诉我:

public Bitmap connectServerForImage(String url) {

          HttpConnection httpConnection = null;
          DataOutputStream httpDataOutput = null;
          InputStream httpInput = null;
          int rc;

          Bitmap bitmp = null;
          try {
           httpConnection = (HttpConnection) Connector.open(url+ ";deviceside=true;ConnectionUID=S TCP");
            rc = httpConnection.getResponseCode();
           if (rc != HttpConnection.HTTP_OK) {
            throw new IOException("HTTP response code: " + rc);
           }
           httpInput = httpConnection.openInputStream();
           InputStream inp = httpInput;
           byte[] b = IOUtilities.streamToBytes(inp);
           EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
           return hai.getBitmap();

          } catch (Exception ex) {
           System.out.println("URL Bitmap Error........" + ex.getMessage());
          } finally {
           try {
            if (httpInput != null)
             httpInput.close();
            if (httpDataOutput != null)
             httpDataOutput.close();
            if (httpConnection != null)
             httpConnection.close();
           } catch (Exception e) {
            e.printStackTrace();

           }
          }
          return bitmp;
         }
于 2012-10-11T08:39:00.747 回答