我需要通过单击按钮将图像从 android 客户端发送到 Restful Web 服务,为此我使用了以下代码
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 类型保存在 MySql 数据库中。 单击另一个按钮后,我从 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 并将其发送给对等方时,或者
- 在您将其转回图像之前,在同行中收到它。
关于java - 无法将从 Restful 服务接收到的 inputStream 解码为 Android 中的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148684/