邏輯:接收到socket之后需要將socket發送的圖片數據保存下來並通知handler更新界面
關鍵代碼:
public void readImage(Socket socket) { try { InputStream in = socket.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); Bitmap bitmap = BitmapFactory.decodeStream(bis);//這個好像是android里的 //首先看看文件是否存在 File f = new File(mfilePath); File bmpFile = new File(mfilePath, "screen.png"); if (!f.exists()) { f.mkdirs(); } if (bmpFile.exists()) { bmpFile.delete(); } FileOutputStream out = new FileOutputStream(bmpFile); bitmap.compress(Bitmap.CompressFormat.PNG, 100,out); bis.close(); in.close(); } catch(Exception ex) { Log.v(TAG, ex.getMessage()); } finally { Message msg = new Message(); msg.what = 1; //說明有圖片更新 mHandler.sendMessage(msg); Log.v(TAG, "接收到圖片,准備handler消息更新界面"); release(); } }
服務器端socket接收所有代碼:
import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.util.Log; /** * 服務器端完整socket接收代碼(接收客戶端的圖片) * @author huqiang * */ public class SocketService implements Runnable{ private static final String TAG = "SZU_TcpService"; private ServerSocket serviceSocket; private boolean SCAN_FLAG = false; // 接收掃描標識 private boolean REV_FLAG = false; // 接收標識 private static String mfilePath = null; // 存放接收文件的路徑 private static Context mContext; private static SocketService instance; // 唯一實例 private Thread mThread; private boolean IS_THREAD_STOP = false; // 是否線程開始標志 private static Handler mHandler; public SocketService() { try { serviceSocket = new ServerSocket(4700); Log.d(TAG, "建立監聽服務器ServerSocket成功"); } catch (IOException e) { Log.d(TAG, "建立監聽服務器ServerSocket失敗"); e.printStackTrace(); } mThread = new Thread(this); } /** * <p> * 獲取TcpService實例 * <p> * 單例模式,返回唯一實例 */ public static SocketService getInstance(Context context,Handler handler,String filepath) { mContext = context; mHandler = handler; mfilePath = filepath; if (instance == null) { instance = new SocketService(); } return instance; } public void setSavePath(String fileSavePath) { Log.d(TAG, "設置存儲路徑成功,路徑為" + fileSavePath); this.mfilePath = fileSavePath; // REV_FLAG=true; } public SocketService(Context context) { this(); mContext = context; } private void scan_recv() { try { Socket socket = serviceSocket.accept(); // 接收UDP數據報 // socket.setSoTimeout(10*1000); // 設置掉線時間 Log.d(TAG, "客戶端連接成功"); //通過子線程來循環讀取socket的消息 ListenClientSocket ls = new ListenClientSocket(socket); ls.start(); } catch (IOException e) { e.printStackTrace(); Log.d(TAG, "客戶端連接失敗"); SCAN_FLAG = false; } } @Override public void run() { Log.d(TAG, "TCP_Service線程開啟"); while (!IS_THREAD_STOP) { if (SCAN_FLAG) { scan_recv(); } } } public void release() { if (null != serviceSocket && !serviceSocket.isClosed()) try { serviceSocket.close(); serviceSocket = null; Log.d(TAG, "關閉socket成功"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while (SCAN_FLAG == true) ;// 直到SCAN_FLAG為false的時候退出循環 SCAN_FLAG = false; IS_THREAD_STOP = true; } public void startReceive() { SCAN_FLAG = true; // 使能掃描接收標識 if (!mThread.isAlive()) mThread.start(); // 開啟線程 } public void stopReceive() { while (SCAN_FLAG == true) ; SCAN_FLAG = false; // 失能掃描接收標識 } /** * 監聽客戶端發送的消息 * @author huqiang * */ class ListenClientSocket implements Runnable { private boolean IsListening = false; private Socket clientSocket; private Thread thread; public ListenClientSocket(Socket s) { this.clientSocket = s; this.thread = new Thread(this); } @Override public void run() { while(clientSocket!=null&&!clientSocket.isClosed()&&IsListening) { readImage(this.clientSocket); } } public void start() { IsListening = true; thread.start(); } public void release() { IsListening = false; if(!clientSocket.isClosed()) { try { clientSocket.close(); clientSocket = null; } catch (IOException e) { e.printStackTrace(); } } } public void readImage(Socket socket) { try { InputStream in = socket.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); Bitmap bitmap = BitmapFactory.decodeStream(bis);//這個好像是android里的 //首先看看文件是否存在 File f = new File(mfilePath); File bmpFile = new File(mfilePath, "screen.png"); if (!f.exists()) { f.mkdirs(); } if (bmpFile.exists()) { bmpFile.delete(); } FileOutputStream out = new FileOutputStream(bmpFile); bitmap.compress(Bitmap.CompressFormat.PNG, 100,out); bis.close(); in.close(); } catch(Exception ex) { Log.v(TAG, ex.getMessage()); } finally { Message msg = new Message(); msg.what = 1; //說明有圖片更新 mHandler.sendMessage(msg); Log.v(TAG, "接收到圖片,准備handler消息更新界面"); release(); } } } }