Java android将图片发送到服务器,我怎么能创建一个框架?

时间:2016-11-15 09:22:59

标签: java android image upload dataformat

如何创建如下所示的框架:

0xB (byte)
0xA (byte)
0xA (byte)
0xD (byte)
width img (short)
height picture (short)
size img in byte  (int)
Binary data PICTURE
0xB (byte)
0xE (byte)
0xE (byte)
0xF (byte)

我不知道如何创建这个框架。

宽度和高度img我就像这样:

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int height=bd.getBitmap().getHeight();
int width=bd.getBitmap().getWidth();

我在这里得到了尺寸:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
    R.drawable.ic_launcher);
Bitmap bitmap = bitmapOrg;
ByteArrayOutputStream stream = new ByteArrayOutputStream();   
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
byte[] imageInByte = stream.toByteArray(); 
long lengthbmp = imageInByte.length;

1 个答案:

答案 0 :(得分:0)

似乎您需要$route['controller_name/sitemap\.xml'] = "controller_name/sitemap"; connection。这样的事情(在您的代码之后):

Socket

您可以搜索更多Socket clientSocket; clientSocket = new Socket(<address_of_server>, <number_of_port>); DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); dos.writeByte((byte)0xB); dos.writeByte((byte)0xA); dos.writeByte((byte)0xA); dos.writeByte((byte)0xD); dos.writeShort(width); dos.writeShort(height); dos.writeInt(lengthbmp); dos.write(imageInByte); dos.writeByte((byte)0xB); dos.writeByte((byte)0xE); dos.writeByte((byte)0xE); dos.writeByte((byte)0xF); ... clientSocket.close(); 个连接示例,例如this

<强>更新

您还可以使用套接字将字符串发送到服务器:

Socket

你应该在单独的(不是UI)线程中完成它。完整的例子是这样的:

clientSocket = new Socket(serverAddr, SOCKET_SERVERPORT);

dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF("Hello");
dos.writeUTF("World!");
...

而且你可以这样使用class SocketClientThread implements Runnable { DataInputStream dis; DataOutputStream dos; String strResponseData; @Override public void run() { try { InetAddress serverAddr = InetAddress.getByName("<address>"); clientSocket = new Socket(serverAddr, <port_number>); dos = new DataOutputStream(clientSocket.getOutputStream()); dis = new DataInputStream(clientSocket.getInputStream()); // now you can write data to stream dos.writeUTF("Hello"); dos.writeUTF("World!"); // you can also read data from stream strResponseData = dis.readUTF(); } catch (UnknownHostException ignore) { } catch (IOException ignore) { } finally{ if (clientSocket != null){ try { clientSocket.close(); } catch (IOException ignore) { } } } } }

SocketClientThread