Outofmemory Error

  • Replies:1
Umer Khan
  • Forum posts: 1

May 19, 2013, 12:10:15 PM via Website

Hi, i am taking a snapshots of screen on the server(in java) side and continously sending them on the client(in Android) side (actually trying to provide the remote view) but the image on client side are not displaying, client run for a while and give exception OutofMemoryError.
The code of screen capture is like this

public void CreateScreenCapture() throws IOException, AWTException
{
screensize = Toolkit.getDefaultToolkit().getScreenSize();
bi = new Robot().createScreenCapture(new Rectangle(0, 0, screensize.width, screensize.height));
byteout = new ByteArrayOutputStream();
ImageIO.write(bi, "jpeg", byteout);
data = byteout.toByteArray();
}

and then this fucntion send screencaptures

public void sendScreenCaptures() throws IOException, AWTException
{
while(true)
{
System.out.println("Screen");
sp.CreateScreenCapture();
out.write(sp.getScreenCapture());
}
}

And on the client side

public void getScreenCapture() throws IOException
{
while(true)
{
img = this.getByteArrayFromStream();
image = BitmapFactory.decodeByteArray(img, 0, img.length);
// image = UpdateScreen.decodeSampledBitmapFromStream(img, width, height);
}
}

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


public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}


public static Bitmap decodeSampledBitmapFromStream(byte[] res,
int reqWidth, int reqHeight)
{

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(res, 0, res.length, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(res, 0, res.length, options);
}


SO PLEASE HELP ME IN THIS

Reply
pekspro
  • Forum posts: 3

May 20, 2013, 10:25:37 AM via Website

When a Bitmap no longer are in use, call Bitmap.recycle to release the bitmap memory. From my experience this is important to do when you create bitmaps frequently. The GC will eventually take care of this but if you create bitmaps often you might get an out out memory exception.

Reply