Why using DecodeStream (stream) takes more time in a thread than in the main thread?

advertisements

I am trying to receive image stream from desktop in the android phone. When I put decodeStream() method in my thread, then put decodestream() into runOnuiThread() to display in the imageView, it takes more than 7 secs. But when I try to directly read image from asset, and convert into inputstream, then decodeStream(), it may take 500ms, I don't know why it happens. The image is 100K.

        Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            SocketConnect = new socketConnect();
            SocketConnect.connectsocket("134.129.125.126", 8080);
            while (true) {
                data = new byte[2048 * 2048];

                try {
                    read = SocketConnect.getInputStream().read(data, 0,
                            data.length);
                    input = SocketConnect.getInputStream();
                    System.out.println("getInputStream()");
                    bitmap = BitmapFactory.decodeStream(input);
                    System.out.println("decodestream()");

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    System.out.println(e);
                }
                runOnUiThread(new Runnable() {
                    public void run() {

                        image.setImageBitmap(bitmap);
                        System.out.println("setImage at less than 500");

                    }
                });
            }
        }
    });
    thread.start();

client side should sent the image very 5 sec. If I try to read same image from asset, Image display immedately.

try {
     inputasset=getAssets().open("good1.jpg");
     Bitmap bit = BitmapFactory.decodeStream(inputasset);
     image.setImageBitmap(bit);
     } catch (IOException e) {
      TODO Auto-generated catch block
     e.printStackTrace();
     }

socketConnect class

    public class socketConnect {
    private Socket clientSocket;
    private InputStream input;
    private Bitmap bMap;

    public InputStream getInputStream() {
        return input;
    }

    public void connectsocket(String ipString, int port)

    {
        System.out.println("starts");

        try {
            // clientSocket = new Socket("134.129.125.172",8080);
            System.out.println("starts");
            clientSocket = new Socket(ipString, port);

            System.out.println("AsyncTask: Connect to 134.129.125.126");
            input = clientSocket.getInputStream();

            System.out.println("AsyncTask: get the inputStream");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


                read = SocketConnect.getInputStream().read(data, 0,
                        data.length);
                input = SocketConnect.getInputStream();
                System.out.println("getInputStream()");
                bitmap = BitmapFactory.decodeStream(input);
                System.out.println("decodestream()");

You read the data from the input stream, then you ask the BitmapFactory to read the data from the input stream. But you already read it! Remove the call to read -- you're stealing data from the BitmapFactory.