简体   繁体   中英

Android receiving image from c++ socket

New to android application: Need some help I am trying to display the image that i have receive from my c++ socket. I am able to receive the image from my server socket and it is displaying the image that i have receive fine. But the emulator force closes after that and i have no idea why.

Android code

private  class Connect extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params){
        int imageSize=921600;
        InputStream in;
        mRun = true;
        try{
            port1 = Integer.parseInt(port);
            client = new Socket(ip, port1);
            try{
                while(mRun){
                    in = client.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte buffer[] = new byte[1024];
                    int remainingBytes = imageSize; //
                    while (remainingBytes > 0) {
                        int bytesRead = in.read(buffer);
                        if (bytesRead < 0) {
                            throw new IOException("Unexpected end of data");
                        }
                        baos.write(buffer, 0, bytesRead);
                        remainingBytes -= bytesRead;
                    }
                    in.close();
                    imageByte = baos.toByteArray();   
                    baos.close();
                    int nrOfPixels = imageByte.length / 3; // Three bytes per pixel.
                    int pixels[] = new int[nrOfPixels];
                    for(int i = 0; i < nrOfPixels; i++) {
                        int r = imageByte[3*i];
                        int g = imageByte[3*i + 1];
                        int b = imageByte[3*i + 2];

                        if (r < 0) 
                            r = r + 256; 

                        if (g < 0) 
                            g = g + 256;

                        if (b < 0) 
                            b = b + 256;

                        pixels[i] = Color.rgb(b,g,r);
                    }
                    Bitmap bitmap = Bitmap.createBitmap(pixels, 640, 480,Bitmap.Config.ARGB_8888);
                    camera.setImageBitmap(bitmap);
                    camera.invalidate();
                }
            } catch (IOException e){}
        }
        catch (UnknownHostException e) {}
        catch (IOException e){}
        return null;
    }
}

emulator force closes after that and i have no idea why

Because doInBackground method run on background thread but you are trying to set bitmap for images from doInBackground here :

camera.setImageBitmap(bitmap);
camera.invalidate();

To update ImageView bitmap from main UI Thread use onPostExecute to call setImageBitmap and invalidate for ImageView because this method run on Main UI Thread when doInBackground method task is completed.

OR

Use runOnUiThread method to call setImageBitmap and invalidate method from doInBackground :

      YourActivityName.this.runOnUiThread(new Runnable() {

           @Override
           public void run() {
              camera.setImageBitmap(bitmap);
              camera.invalidate();
            }
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM