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

Android 服务器显示数据,但不显示图像,android,Android,我正在捕获屏幕并将图像转换为字节数组,然后发送到套接字连接。服务器显示数据,但不显示图像 ` 公共类ImageToByte活动扩展活动{ boolean connected = false; public ByteArrayOutputStream bos; public char[] tempChar1 = new char[15]; //public char[] finalValue; public byte[] buffer; byte[] b9; String t

我正在捕获屏幕并将图像转换为字节数组,然后发送到套接字连接。服务器显示数据,但不显示图像

` 公共类ImageToByte活动扩展活动{

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;
}
boolean connected=false;
公共ByteArrayOutputStream bos;
public char[]tempChar1=新字符[15];
//公共特征[]最终价值;
公共字节[]缓冲区;
字节[]b9;
字符串tempStr,s;
公共静态最终字符串mCommand1=“xxxxxxxxxx”;
公共静态最终字符串mCommand2=“yyyyyyyyyyyyyyyyyyyyyyyyyyyyy”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮btn=(按钮)findViewById(R.id.btn);
btn.setOnClickListener(新的OnClickListener(){
//@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
视图=v.getRootView();;
view.setDrawingCacheEnabled(true);
位图largeIcon=view.getDrawingCache();
bos=新的ByteArrayOutputStream();
压缩(位图.CompressFormat.JPEG,100,bos);
buffer=bos.toByteArray();
试一试{
s=新字符串(缓冲区,“UTF8”);
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//Log.w(“图像转换”,bos.toString());
//Log.w(“图像转换”,Arrays.toString(bos.toByteArray());
Log.w(“图像转换”,String.valueOf(buffer.length));
Log.w(“图像转换1”,s);
stringToBytesASCII();
连接();
}
});
}
公共字节[]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=新字节[tempChar1.length];
Log.w(“abcdefgh”,String.valueOf(tempChar1.length));
对于(int i=0;i
}

字符串到二进制(字节[]字节)
{
StringBuilder sb=新的StringBuilder(bytes.length*Byte.SIZE);
对于(int i=0;isb.append((bytes[i/Byte.SIZE]您应该直接将字节缓冲区写入outputstream。现在您正在首先转换为字符串,甚至转换为utf-8。这没有意义,因为图像不能用字符串表示。因此,请删除所有字符串,并在buffer=bos.toByteArray()之后写入缓冲区out。您不能使用tempStr。

这是因为您直接将字节写入输出流。您的服务器端代码应将这些字节转换为映像文件。我的服务器端代码已准备好转换这些字节……单值是什么意思?
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();
}

}