我开发了聊天应用程序。要在聊天中传输图像,我们首先将其上传到内容模块,然后将该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
中ChatActivity.java
private ChatController.OnMessageReceivedListener onMessageReceivedListener = new ChatController.OnMessageReceivedListener() { public void onMessageReceived(final Message message) { final String from = message.getFrom().split("-")[0]; final String messageString = message.getBody(); Log.v("----msg from----","from ap "+ from+"-----"+friend.getId().toString()); if(from.equalsIgnoreCase(friend.getId().toString())) { if(messageString.startsWith(">>In thread"); URL url = new URL(imageUrl); Log.v("---CONNECTION url---","In thread---"+url); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); String fileDir=null; File file=null; File ffile; if(fileType==SELECT_IMAGE) { fileDir=SDCardRoot+"/Globr/Images"; } ffile=new File(fileDir); if(!ffile.exists()) { ffile.mkdirs(); } file = new File(fileDir,filename); Log.v("---folder file name---","image folder fil created---"+filename); if(!file.exists()) { file.createNewFile(); } FileOutputStream fileOutput = new FileOutputStream(file); InputStream inputStream = urlConnection.getInputStream(); int totalSize = urlConnection.getContentLength(); int downloadedSize = 0; byte[] buffer = new byte[1024]; int bufferLength = 0; while ( (bufferLength = inputStream.read(buffer)) > 0 ) { fileOutput.write(buffer, 0, bufferLength); downloadedSize += bufferLength; Log.v("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ; } fileOutput.close(); Log.v("File:-------","file is : "+file); if(downloadedSize==totalSize) filepath=file.getAbsolutePath(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { filepath=null; e.printStackTrace(); } } }.start(); } @Override public void onComplete(Result result, Object 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):
我们无法得到答复。
答案 0 :(得分:2)
onMessageReceived 方法可能会调用非主线程或类似的东西
尝试在主线程上调用代码