我们正在android 2.3.3中进行聊天应用程序。我们希望使用套接字编程将图像从一个模拟器发送到另一个模拟器。我们是Android的新手,而不是找到如何做到这一点。在这方面的任何帮助都将是可观的。 提前致谢
答案 0 :(得分:0)
使用动作选择从图库中获取图像,如下所示:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
这将允许您启动图库应用程序来选择照片: 在OnActivityResult中,你会收到一张照片的照片:
处理此uri以从中获取字节数组:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
//Open a Socket Connection to send photo
Socket socket=new Socket("ip", portNo);
OutputStream os=socket.getOutputStream();
os.write(byteArray);
os.flush();
//Read response by getting input stream from socket.
}
}
}