Android将图片发送到服务器

时间:2015-12-16 17:06:57

标签: java android sockets server

当我尝试将手机拍摄的照片(大约2Mb)发送到我的服务器时,我得到的图像中只有很小一部分具有可怕的分辨率。图像选自图库。这是因为图片太大(分辨率和大小)?这是客户方:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
startActivityForResult(getIntent, PICK_IMAGE);

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            //Display an error
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    server = new Socket("192.168.92.114", 4444);
                    OutputStream outputStream = server.getOutputStream();

                    in = getContentResolver().openInputStream(data.getData());
                    // Write to the stream:
                    byte[] buffer = new byte[1024]; // 1KB buffer size
                    int length = 0;
                    while ((length = in.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, length);
                    }
                    outputStream.flush();

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                    if (in != null)
                        try {
                            in.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    try {
                        server.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        }).start();

    }

}

从服务器:

BufferedImage img = ImageIO.read(ImageIO.createImageInputStream(clientSocket.getInputStream()));
frame.getContentPane().add(new JLabel(new ImageIcon(img)));

0 个答案:

没有答案