服务器显示数据但不显示图像

时间:2013-02-05 17:27:38

标签: android

我正在捕获我的屏幕并将该图像转换为字节数组并发送到套接字连接.Server显示数据但不显示图像。

` 公共类ImageToByteActivity扩展了Activity {

boolean connected = false;
 public ByteArrayOutputStream bos;

 public char[] tempChar1 = new char[15];
 //public char[] finalValue;
 public byte[] buffer;


  byte[] b9;
  String tempStr,s;
  public static final String mCommand1 = "xxxxxxxxxxx";
  public static final String mCommand2 = "yyyyyyyyyyyyyyyyyyyyy";
  /** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            Button btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener(){

                //@Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    View view = v.getRootView();;

                   view.setDrawingCacheEnabled(true);
                   Bitmap largeIcon = view.getDrawingCache();

          bos = new ByteArrayOutputStream(); 
          largeIcon.compress(Bitmap.CompressFormat.JPEG, 100 , bos);

          buffer = bos.toByteArray();

                       try {
                        s = new String( buffer,"UTF8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                       //Log.w("Image Conversion",bos.toString());

                       // Log.w("Image Conversion",Arrays.toString(bos.toByteArray()));
                        Log.w("Image Conversion", String.valueOf(buffer.length));  
                        Log.w("Image Conversion1", s);
                      stringToBytesASCII();
                     connection();

                }
            });

        }
public  byte[] stringToBytesASCII() {
    String pq = String.valueOf(buffer.length);
     tempChar1[0] = 'K';
     tempChar1[1] = 'U';
     tempChar1[2] = 'M';
     tempChar1[3] = 'R';
     tempChar1[4] = 'H';
     tempChar1[5] = 'I';
     tempChar1[6] = 'M';
     tempChar1[7] = 'A';
     tempChar1[8] = 'N';
     tempChar1[9] = 'S';
     tempChar1[10] = 'H';
     tempChar1[11] = 'U';

    b9 = new byte[tempChar1.length]; 
    Log.w("abcdefgh",String.valueOf(tempChar1.length));
    for (int i = 0; i < b9.length; i++) {
      b9[i] = (byte) tempChar1[i];
      try {
            tempStr = new String( b9,"UTF8");
            Log.w("abcd",tempStr);
            Log.w("abcd",String.valueOf(tempStr.length()));


        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }    
     }


     return b9;
    }

//-------------------------------------------------------------

public void connection(){
try {       
        InetAddress serverAddr = InetAddress.getByName("ip");
    Log.d("ClientActivity", "C: Connecting...");
    Socket socket = new Socket(serverAddr, port);
    connected = true;

        try {
            Log.d("ClientActivity", "C: Sending command.");
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                        .getOutputStream())), true);
                // where you issue the commands
            Log.w("gdfgdf",tempStr);
                out.println(mCommand1);
                out.flush();
                Log.d("ClientActivity", "C: Sent.");

              BufferedInputStream inX1 = new BufferedInputStream(socket.getInputStream());
              System.out.println("Server says " + inX1.available());
              Log.d("ClientActivity", "C: Received.");

              while (connected) {

                BufferedOutputStream out3 = (BufferedOutputStream) socket.getOutputStream();
                //out.write("Hello World\n".getBytes(charSet));
                out.write(tempStr);
                out.write(s);
                out.flush();
                Log.d("ClientActivity", "C: Sent Second.");
                //Log.d("value",s);
              }
        } catch (Exception e) {
            Log.e("ClientActivity", "S: Error", e);
        }
    //}
    socket.close();
    Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
    Log.e("ClientActivity", "C: Error", e);
    connected = false;
}

}

String toBinary( byte[] bytes )
{
    StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
    for( int i = 0; i < Byte.SIZE * bytes.length; i++ )
        sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
    return sb.toString();
}

}

`

注意: - 如果我显示byte [],它只包含单个值。

1 个答案:

答案 0 :(得分:0)

您应该直接将您的字节缓冲区写入输出流。现在你首先转换为字符串,甚至是utf-8。由于图像不能用字符串表示,所以没有任何意义。所以取消所有字符串并在buffer = bos.toByteArray()之后写入缓冲区。你不能使用tempStr。