1

我开发了聊天应用程序。要在聊天中传输图像,我们首先将其上传到内容模块,然后将该 ID 共享给接收者。如果出现问题,我们会调用 messagelistner 中的 getFile。但是,将相同的代码放在该方法之外可以正常工作。请看下面的代码:


MyChatController.java


  private MessageListener messageListener = new MessageListener() {
    @Override
    public void processMessage(Chat chat, Message message) {
            System.out.println(String.format(">>> Message received (from=%s, to=%s): %s",from, to, message.getBody()));

        if (onMessageReceivedListener != null)
        {
            onMessageReceivedListener.onMessageReceived(message);
        }
     }
   };

这个方法的实现在 ChatActivity

聊天活动.java

私人 ChatController.OnMessageReceivedListener onMessageReceivedListener = new ChatController.OnMessageReceivedListener()
 {

        public void onMessageReceived(最终消息消息)
        {
            最终字符串 from = message.getFrom().split("-")[0];
            最终字符串 messageString = message.getBody();
            Log.v("----msg from----","from ap "+ from+"-----"+friend.getId().toString());
            if(from.equalsIgnoreCase(friend.getId().toString()))
            {   

                if(messageString.startsWith(">>在线程中");
                                    URL url = 新 URL(imageUrl);
                                    Log.v("---CONNECTION url---","线程中---"+url);
                                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                                    urlConnection.setRequestMethod("GET");
                                    urlConnection.connect();                
                                    字符串文件目录=空;
                                    文件文件=空;
                                    文件文件;
                                    如果(文件类型==SELECT_IMAGE)
                                    {
                                        fileDir=SDCardRoot+"/Globr/Images";
                                    }       
                                    ffile=新文件(fileDir);
                                    if(!ffile.exists())
                                    {
                                        ffile.mkdirs();
                                    }           
                                    文件 = 新文件(文件目录,文件名);
                                    Log.v("---文件夹文件名---","图像文件夹文件创建---"+文件名);
                                    if(!file.exists())
                                    {
                                        文件.createNewFile();
                                    }


                                    FileOutputStream fileOutput = new FileOutputStream(file);
                                    InputStream inputStream = urlConnection.getInputStream();
                                    int totalSize = urlConnection.getContentLength();
                                    int 下载大小 = 0;
                                    字节[]缓冲区=新字节[1024];
                                    int 缓冲区长度 = 0;
                                    而 ( (bufferLength = inputStream.read(buffer)) > 0 )
                                    {               
                                        fileOutput.write(buffer, 0, bufferLength);                
                                        下载大小 += 缓冲区长度;               
                                        Log.v("进度:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
                                    }           
                                    文件输出.close();
                                    Log.v("文件:-------","文件为:"+文件);
                                    if(downloadedSize==totalSize) 文件路径=file.getAbsolutePath();  
                                }
                                捕获(MalformedURLException e)
                                {
                                    e.printStackTrace();
                                }
                                捕获(IOException e)
                                {
                                    文件路径=空;
                                    e.printStackTrace();
                                }
                            }
                        }。开始();

                    }

                    @覆盖
                    公共无效onComplete(结果结果,对象o){
                    }
                });
            }
        }
    }

日志:

04-19 18:02:26.163: I/System.out(25108): >>> Message received (from=110918, to=115738): <file_image 40418

04-19 18:02:26.193: V/--------IMAge--------(25108): --ID 40418--lenth--5---40418

04-19 18:02:26.193: D/QBASDK RestRequest(25108): 

=========================================================

04-19 18:02:26.193: D/QBASDK RestRequest(25108): === REQUEST ==== 09a23d0f-5fa9-4306-94e8-9ebc8c44c9ec ===

04-19 18:02:26.193: D/QBASDK RestRequest(25108): REQUEST

04-19 18:02:26.193: D/QBASDK RestRequest(25108):     GET https://api.quickblox.com/blobs/40418.json

04-19 18:02:26.193: D/QBASDK RestRequest(25108): HEADERS

04-19 18:02:26.193: D/QBASDK RestRequest(25108): QuickBlox-REST-API-Version=0.1.1

04-19 18:02:26.193: D/QBASDK RestRequest(25108): QB-SDK=Android 0.8.1

04-19 18:02:26.193: D/QBASDK RestRequest(25108): QB-Token=9c0e70b1c1b2a31bb497f6672e72563fa570d492

04-19 18:02:26.193: D/QBASDK RestRequest(25108):PARAMETERS

04-19 18:02:26.193: D/QBASDK RestRequest(25108):INLINE

04-19 18:02:26.193: D/QBASDK RestRequest(25108):GET https://api.quickblox.com/blobs/40418.json

04-19 18:02:37.304: D/SMACK(25108): 06:02:37 PM SENT (1118266312):  

04-19 18:02:43.050: D/GestureDetector(25108): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0

04-19 18:03:03.360: D/GestureDetector(25108): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0

04-19 18:03:07.304: D/SMACK(25108): 06:03:07 PM SENT (1118266312): 

我们无法得到回应。

4

1 回答 1

1

onMessageReceived方法可能会调用非主线程或类似的东西

尝试在主线程上调用您的代码

于 2013-05-09T11:57:51.570 回答