在本應用當中添加了一個用戶註冊的功能,用戶註冊完之後,在服務器端將保存用戶的註冊的信息,當下一次再進入應用時將自動嘗試與服務器的連接,類似於我們常用的聊天工具,具體的註冊方式爲:填寫ip地址、端口號、輸入用戶名、選擇頭像,單擊註冊按鈕即可完成註冊,前提是服務器必須要開啓啦。
上傳頭像時提供了本地相冊上傳和攝像頭上傳兩種方式,同時對圖像進行了裁剪,以適合頭像的大小。
/**
* 裁剪圖片方法實現
*
* @param data
*/
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面這個crop=true是設置在開啓的Intent中設置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUE_CODE_CROP);
}
客戶端通過開啓線程建立socket連接完成註冊功能,在這裏完成註冊後關閉該socket,已經不需要了。註冊完成後將在服務器端模擬數據庫用xml文件保存用戶的註冊信息。
/**
* 註冊用戶
* @param cropBitmap
* @param ip
* @param port
* @param name
* @throws IOException
* @throws Exception
*/
public void registerUser(Context context, Bitmap cropBitmap, String ip, String port,
String name) throws IOException {
UserService userService = new UserService(context);
ByteArrayOutputStream byteoutput = new ByteArrayOutputStream();
cropBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteoutput);
byte datas[] = byteoutput.toByteArray();
DataOutputStream output = null;
DataInputStream input = null;
Socket socket = null;
long rowId = 0;
try {
User user = new User(ip, port, name, cropBitmap, 1);
SocketAddress socAddress = new InetSocketAddress(InetAddress.getByName(ip), Integer.parseInt(port));
socket = new Socket();
socket.connect(socAddress, 3000);
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(socket.getInputStream());
String flagLine = ContentFlag.REGOSTER_FLAG + name;
output.writeUTF(flagLine);
output.write(datas);
//讀取服務器分配給用戶的唯一標識
rowId = Long.valueOf(input.readUTF());
user.setId(rowId);
userService.insertUser(user);
} catch (IOException e) {
throw new IOException("fail connect to the server");
} finally {
try {
if(byteoutput!= null)
byteoutput.close();
if (output != null)
output.close();
if (input != null)
input.close();
if (socket != null)
socket.close();
if (cropBitmap != null)
cropBitmap.recycle();
} catch (IOException e) {
e.printStackTrace();
}
}
}
註冊頁面效果圖:
完整資源代碼下載地址:http://download.csdn.net/detail/jiangliloveyou/6457969