傳送圖片,開始信息不全,修改後總算正確傳輸。
java 寫的服務器,把讀取的圖片保存到本地。
public class AndroidRunable implements Runnable {
Socket socket = null;
public AndroidRunable(Socket st) {
this.socket = st;
}
public void run() {
String line = null;
InputStream input;//讀取客戶端信息
OutputStream output;//向客戶端發送數據
String str = "hello android";
try {
input = socket.getInputStream();
BufferedReader bff = new BufferedReader(
new InputStreamReader(input));
// 獲取客戶端的信息
String img = "";
while ((line = bff.readLine()) != null) {
img += line;
}
String[] imgs = img.split("採集圖片");
for (int i = 0; i < imgs.length; i++) {
System.out.println("img>>>" + imgs[i]);
Decoder d = Base64.getDecoder();
byte2image(d.decode(imgs[i]), "kk" + i);
}
bff.close();
input.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// byte數組到圖片
public void byte2image(byte[] data, String path) {
if (data.length < 3 || path.equals(""))
return;
try {
FileOutputStream imageOutput = new FileOutputStream(new File(
"C:\\Users\\Administrator\\Desktop\\socket\\" + path
+ ".png"));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in "
+ path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
}
服務器循環判斷是不是客戶端要建立連接。
public class AndroidService {
public static void main(String[] args) {
Socket st = null;
try {
ServerSocket socket = new ServerSocket(2584);
while (true) {
st = socket.accept();
new Thread(new AndroidRunable(st)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
st.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
android 客戶端,我讀取的 drawable裏的圖片
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.test1);
String strbm = Base64.encodeToString(BitmapToBytes(bmp), Base64.DEFAULT);
Bitmap bmp1 = BitmapFactory.decodeResource(res, R.drawable.test2);
String strbm2 = Base64.encodeToString(BitmapToBytes(bmp1), Base64.DEFAULT);
Bitmap bt = Bytes2Bimap(Base64.decode(strbm,Base64.DEFAULT));
System.out.println(strbm + "bt" + bt);
ImageView iv= (ImageView) findViewById(R.id.img_socket);
iv.setImageBitmap(bt);
new SocketThread("採集圖片"+strbm+"採集圖片"+strbm2, MainActivity.this, socket).start();
bitmap轉換爲byte的方法
public byte[] BitmapToBytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
try {
socket = new Socket();
//socket.connect(new InetSocketAddress("192.168.1.108", 2584), 5000);
socket.connect(new InetSocketAddress("192.168.1.105", 2584), 5000);
//獲取輸入輸出流
OutputStream ou = socket.getOutputStream();
//BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//讀取發來服務器信息
buffer = "";
//向服務器發送信息
ou.write((sendMes).getBytes("UTF-8"));
ou.flush();
bundle.putString("msg", buffer.toString());
msg.setData(bundle);
//發送信息,修改ui線程中的組件
myHandler.sendMessage(msg);
// bff.close();
ou.close();
socket.close();
} catch (SocketTimeoutException aa) {
//連接超時
bundle.putString("msg", "服務器連接失敗!請檢查網絡是否打開");
msg.setData(bundle);
myHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}