1
我的代碼旨在用按鈕來按下UI按鈕時更新帶有來自服務器的圖像的ImageView。Android:BitmapFactory.decodeStream()在第一次成功後返回NULL
下面顯示的客戶端代碼是按下按鈕時運行的Runnable。該服務器是一個桌面Java應用程序,ffmpeg在後臺運行,不斷使用網絡攝像頭中的圖像更新image.png。當在Android應用上按下按鈕時,Android應用嘗試從服務器接收image.png,並且由於ffmpeg不斷更新此圖像,它應該是服務器網絡攝像機拍攝的最新圖像。
我的問題是,第一個按鈕按下顯示正確的圖像,但每一個後續的按鈕將剛剛清除我的ImageView。每當我第一次調用它時,BitmapFactory.decodeStream()都會返回null。
客戶端(運行按下按鈕時):
InputStream inputStream = s.getInputStream();
img = BitmapFactory.decodeStream(inputStream);
jpgView.setImageBitmap(img);
jpgView.invalidate();
服務器端:
ServerSocket sock = new ServerSocket(PORT_NUMBER);
Socket clientSocket = sock.accept();
for (;;) {
File f = new File("C:/folder/image.png");
FileInputStream fileInput = new FileInputStream(f);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInput);
OutputStream outStream = clientSocket.getOutputStream();
try {
byte[] outBuffer = new byte[fSize];
int bRead = bufferedInput.read(outBuffer, 0, outBuffer.length);
outStream.write(outBuffer, 0, bRead);
outStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
bufferedInput.close();
}
}
沒有看到您的更多的代碼,它只是一個猜測,但我的猜測是你不重新創建你的客戶端的「S」,所以你重用連接,服務器只發送一個IMG到新的連接。 –