无法在Android中将从restful服务接收到的inputStream解码为位图



我有一个要求,即点击按钮即可将图像从android客户端发送到Restful Web Service,为此我使用了以下代码

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.myImage);  
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    encodedString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
reqParams.put("image",encodedString);
client.post(IMAGE_POST_URL, reqParams, new AsyncHttpResponseHandler() {....});

我可以将图像发送到restful服务,并将其作为Blob类型保存在MySqlDB中。单击另一个按钮后,我将从restful web服务接收作为InputStream的图像。但我无法转换为位图并使用以下代码在屏幕上显示。有人能告诉我哪里做错了吗。

URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
inputStream = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
imgView.setImageBitmap(imageBmp);

任何帮助都将不胜感激。。

以下是我正在使用的Rest服务方法。。

@POST
@Path("/uploadImage1")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response uploadImage1(@FormParam(value = "image") String image) {     
InputStream is = new ByteArrayInputStream(image.getBytes());
.....(MySQL Code to insert as BLOB)}
@GET
@Path("/getImage")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getImage() {....
Response.ok(blob.getBinaryStream(), MediaType.APPLICATION_OCTET_STREAM).build();}

根据您对注释的回答,您似乎从未执行base64解码步骤。你需要在某个地方这样做:要么

  • 在将Blob插入数据库时,或者
  • 当您检索Blob并将其发送给对等方时,或者
  • 在您将其转换回图像之前,在对等体中接收到它的点

最新更新