【问题标题】:can not retrieve compressed bitmap from server无法从服务器检索压缩位图
【发布时间】:2014-11-20 13:57:10
【问题描述】:

所以我试图通过休息服务器存储压缩位图的 byte[] 数组。我首先从 imageView 中检索源文件并将其转换为位图,然后将其转换为字节数组,然后将原始字节发送到服务器并保存:

PhotoSvcApi photoService= RestServer.getInstance();
...
                            Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            byte[] imageBytes = baos.toByteArray();

                            Long newId = photoService.addPhotoData(imageBytes);

在服务器端,我使用 springframe 工作来映射处理程序以将数据保存和检索到服务器。根据我的日志和测试,我能够成功地从我的客户保存和检索数据。这是从服务器检索数据到android客户端的方法。

@RequestMapping(value = PhotoSvcApi.IMAGE_DATA_PATH, method = RequestMethod.GET)
    public @ResponseBody
    void getData(@PathVariable(value = PhotoSvcApi.ID_PARAMETER) Long id,
            HttpServletResponse response) {

        byte[] source = imageData.get(id);

        try {
            response.getOutputStream().write(source);
        } catch ( IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

如您所见,我将字节放入响应正文中的 OutputStream 中。接下来,是我认为错误发生的地方,因为我无法成功重新创建压缩位图。我正在使用改造来访问服务器。

public void getDataFromServer(long id){

        CallableTask.invoke(new Callable<Bitmap>() {

            @Override
            public Bitmap call() throws Exception {
                // TODO Auto-generated method stub

                Intent intent = getActivity().getIntent();
                long giftId = intent.getLongExtra(ID_KEY, -1);
                Log.e("detail", String.valueOf(giftId));





                PhotoSvcApi photoService= RestServer.getInstance();
                Response response = photoService.getData(giftId); // retrofit streaming



                Bitmap bitmap =  BitmapFactory.decodeStream(response.getBody().in());
                if(bitmap == null){
                    Log.e("detail", "bitmap is in fact null - fix it");
                }

我真的不认为它与服务器有关,因为我的@post REST 方法发送的数据量与@get 方法在其主体中返回的数据量完全相同

11-19 23:47:58.933: D/Retrofit(3405): ---> HTTP POST http://10.0.2.2:8080/giftdata
11-19 23:47:59.611: D/Retrofit(3405): ---> END HTTP (1701907-byte body)
11-19 23:48:07.081: D/Retrofit(3405): <--- HTTP 200 http://10.0.2.2:8080/giftdata (7474ms)
11-19 23:48:07.081: D/Retrofit(3405): : HTTP/1.1 200 OK
11-19 23:48:07.101: D/Retrofit(3405): <--- END HTTP (1-byte body)
...
11-19 23:48:54.324: D/Retrofit(3405): ---> HTTP GET http://10.0.2.2:8080/giftdata/4/data

11-19 23:48:54.332: D/Retrofit(3405): ---> END HTTP (no body)
11-19 23:48:54.372: D/Retrofit(3405): <--- HTTP 200 http://10.0.2.2:8080/giftdata/4/data (39ms)
11-19 23:48:54.372: D/Retrofit(3405): : HTTP/1.1 200 OK
11-19 23:48:57.671: D/Retrofit(3405): <--- END HTTP (1701907-byte body)

【问题讨论】:

    标签: android bitmap compression spring-boot retrofit


    【解决方案1】:

    您在 void 方法上有 @ResponseBody,这实际上没有任何意义,但可能 Spring 是按字面意思对待它并且什么都不发送。您根本不必将字节写入响应,只需从方法中返回它们即可。

    【讨论】:

    • 是的,显然是这样。位图不再返回 null。谢谢你。现在我只需要弄清楚为什么 imageView 不能正确显示位图。
    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多