为什么应用程序在调试期间有效但在运行时无效?

时间:2011-06-19 23:24:32

标签: android debugging bitmap client-server bytearray

我在Android中有一个客户端,在C#中有服务器,他们通过套接字进行通信。 我有这个问题 - 如果我在调试模式下运行我的客户端应用程序并在正确的位置放置一个断点 - 它完美地工作,但没有它它没有。 客户端将图像的地址发送到服务器,服务器创建它的缩略图,将其转换为byte []并将其发回。客户端获取byte [],将其转换回图像并显示它。 我还发现,当它没有得到正确的字节[]时,它的大小是2896,有时是1448,无论发送的数组的原始大小是什么。

这是客户:

private void connectSocket(String a){ 

    try { 
        InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); 
        Socket socket = new Socket(serverAddr, 4444); 
        String message = a;
        flag = 0;
        if(a.indexOf(".jpg")>0){
            flag = 1;
        }

        ListItems.clear();
        if(!a.equalsIgnoreCase("get_drives"))){
                    //.....
        }
        if(!ListItems.isEmpty()){               
            if(ListItems.get(0).matches("^[A-Z]{1}:$")){
                ListItems.set(0, ListItems.get(0)+"\\");
            }
        }
        PrintWriter out = null;
        BufferedReader in = null;
        InputStream is = null;          
        try { 
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            is = socket.getInputStream();
            out.println(message);

            String text = "";
            String finalText = "";
            if(flag!=1){
                while ((text = in.readLine()) != null) {
                    finalText += URLDecoder.decode(text, "UTF-8");
                    }
                String[] result = finalText.split("#");
                for(int i = 0; i<result.length; i++)
                    ListItems.add(result[i]);
                }
            if(flag ==1){                   

                    byte[] buffer = new byte[9999];
                 //placing breakpoint at the next line or before it makes it work fine                     
                    int size = is.read(buffer);
                //but placing a breakpoint at the line below doesn't make it work
                //it starts getting another byte array 
                    byte[] bufffer2 = new byte[size];
                    for(int g = 0; g<size;g++){
                        bufffer2[g] = buffer[g];
                    }
                    //is.read(bufffer2);
                    //int read = is.read(buffer);

                image = (ImageView)findViewById(R.id.imageView1);
                //while ((text = in.readLine()) != null) {
                //  byte[] b = in.readLine().getBytes();
                    Bitmap bmp=BitmapFactory.decodeByteArray(bufffer2,0,bufffer2.length);                       
                    image.setImageBitmap(bmp);
                //}
            }
            adapter.notifyDataSetChanged(); 

        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            socket.close(); 
        } 

    } catch (UnknownHostException e) { 
        Log.e("TCP", "C: UnknownHostException", e); 
        e.printStackTrace(); 
    } catch (IOException e) {  
        Log.e("TCP", "C: IOException", e); 
        e.printStackTrace(); 
    }       
}

这是服务器:

public class serv {
public static void Main() {
try {
    IPAddress ipAd = IPAddress.Parse("192.168.1.2");
    TcpListener myList=new TcpListener(ipAd,4444);

   m:      
    myList.Start();

    Socket s=myList.AcceptSocket();

    byte[] b=new byte[100];
    int k=s.Receive(b);

    char cc = ' ';
    string test = null;
    Console.WriteLine("Recieved...");
    for (int i = 0; i < k-1; i++)
    {
        Console.Write(Convert.ToChar(b[i]));
        cc = Convert.ToChar(b[i]);
        test += cc.ToString();
    }

    string[] response = null;
    ASCIIEncoding asen = new ASCIIEncoding();
    switch (test)
    {
        default:
            MyExplorer(test, s);
            break;

    }

    s.Close();
    myList.Stop();
    goto m;

}
catch (Exception e) {
    Console.WriteLine("Error..... " + e.StackTrace);        
}    
}

public static void MyExplorer(string r, Socket s){
    Image imgThumb = null;
    if (r.Contains(".jpg"))
    {
        Image image = null;
        image = Image.FromFile(r);
        // Check if image exists
        if (image != null)
        {
            imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
            s.Send(imageToByteArray(imgThumb));
            byte[] b = imageToByteArray(imgThumb);
        }
        return;
    }

}

public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

}

2 个答案:

答案 0 :(得分:5)

InputStream中的read(byte [])方法读取的字节数与当前可用的字节数相同。它并不一定意味着以后不会有更多的字节可用。所以你需要做一些像

这样的事情
while(true) {
  int s  = is.read(buffer);
  if(s == -1) break;

  // else
  add read buffer to image array
}

extract image from image array

其中“image array”是一些字节数组,您可以继续添加读取缓冲区,直到到达流的末尾。

您的代码使用断点的原因是,当您通过调试器时,整个流都可用,而在非调试时,整个流在您执行读取时尚不可用。

答案 1 :(得分:1)

差异可能是因为在调试器上,您可以减慢和停止应用程序 - 这为服务器提供了响应您的呼叫的时间。看起来你没有使用任何异步方法来调用连接套接字(好吧,我无论如何都看不到你代码中的任何证据)。
要解决此问题,您应该尝试扩展AsyncTask对象examples here
编辑:@Carsten可能有正确的解决方案,但如果你还没有,你应该仍然使用AsyncTask。

相关问题