尝试连续显示图像时出现OutOfMemoryException

时间:2013-05-12 10:24:46

标签: android out-of-memory

我正在尝试使用screenCapture显示我从服务器端获取的图像,但在客户端(Android模拟器)上显示的图像在通过异常“OutOfMemoryException”运行一段时间后没有显示任何图像。所以我需要一些帮助。这是代码。

服务器

  public class Server
{
  private Connection con;
  private ScreenCapture sp;
  private Socket soc;
  private DataOutputStream out;
  private DataInputStream in;

public Server() throws IOException, AWTException
{
    con = new Connection();
    sp = new ScreenCapture();  
}

public void init() throws IOException, AWTException

{
    con.setPort(3838);
    ServerSocket serversocket = new ServerSocket(3838);
    soc = serversocket.accept();
    System.out.println("Accepted");
    out = new DataOutputStream(soc.getOutputStream());
    in = new DataInputStream(soc.getInputStream());
    while(true)
    {
    sp.CreateScreenCapture();
    out.write(sp.getScreenCapture());
    }

}

public void closeConnection() throws IOException
{
    soc.close();
}

public static void main(String[] arg)
{

        Server server = null;

    while(true)
    {
        try
        {
            server = new Server();
            server.init();

        }
        catch (IOException ex)
        {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);

        }
        catch (AWTException ex)
        {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            try
            {
                server.closeConnection();
            }
            catch (IOException ex)
            {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }


}

}

客户端

  public class UpdateScreen extends View
{

private DataInputStream input;
private DataOutputStream output;
private Socket soc;
private Bitmap image;



public UpdateScreen(Context context) throws UnknownHostException, IOException 
  {
    super(context);
    // TODO Auto-generated constructor stub
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);
    init();
}

public void init() throws UnknownHostException, IOException
{

    soc = new Socket(InetAddress.getByName("10.0.2.2"), 3838);
    input = new DataInputStream(soc.getInputStream());
    while(true)
    {
    byte[] img = this.getByteArrayFromStream();
    image = BitmapFactory.decodeByteArray(img, 0, img.length);

    }

}


protected void onSizeChanged(int w, int h, int oldw, int oldh) 
   {
    // TODO Auto-generated method stub

     image = Bitmap.createScaledBitmap(image, w, h, false);

    }


public byte[] getByteArrayFromStream() throws IOException
{
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nread;
    byte[] data = new byte[1042];
    while((nread = input.read(data, 0, data.length)) != -1)
    {
       buffer.write(data, 0, nread);    
    }

     return buffer.toByteArray();
}


@Override
protected void onDraw(Canvas canvas) 
{
    // TODO Auto-generated method stub
    invalidate();
    super.onDraw(canvas);
    canvas.drawBitmap(image, 0, 0, null);

}

}

2 个答案:

答案 0 :(得分:0)

服务器连续发送图像,客户端在开始绘制之前尝试接收所有数据(无限量)。

更好的是例如轮询方法,意味着客户端请求新映像,服务器提供单个映像。

仍然存在一些问题:服务器必须发送图像数据的长度(或在每个图像之后关闭连接),以便客户端知道何时应该停止等待数据并开始处理而onDraw()不应该调用invalidate()因为这可能会导致太频繁或太少进一步调用onDraw()(如果它可以工作)。

答案 1 :(得分:0)

如果您使用ImageView来显示图像,您可能不需要服务器正在发送的巨大图像。

你可以做几件事:

  1. 使用像Aquery这样的库并将其ImageLoading与您的视图相关联
  2. 自己努力工作 - 参考Android文档 - 有效显示位图