Reading Raw data from Bitmap files
<p>I'm trying to decode a .jpg file into a bitmap and reading the raw data from the Bitmap File. 
The following is the code snippet of my app.</p>

<pre><code> File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");

  int top = 450;
  int left = 0;

  int right= 1450;
  int bottom = 2592;

  int width = right-top;
  int height = bottom-left;

  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;

  bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

  Bitmap bitmapScaled = Bitmap.createBitmap(bitmap, top, left, width, height);
  bitmap.recycle();

  ByteBuffer buffer  = ByteBuffer.allocate(width*height*4);
  bitmapScaled.copyPixelsToBuffer(buffer);

  bitmapScaled.recycle();

  File file2 = new File(Environment.getExternalStorageDirectory(),"decodeFile");

  FileOutputStream out = new FileOutputStream(file2.getAbsolutePath());
  out.write(buffer.array());
</code></pre>

<p>In the above snippet, im trying to read the raw data from the Bitmap file into ByteBuffers and storing into a newly created file(decodeFile) in the SDCARD. </p>

<p>When i see the data in the "decodeFile" half of the data will becomes null, and the data is improper.</p>

<p>The above is one type of method of reading the raw data using Bitmap class methods.</p>

<p>When i follow the same thing by using the below code snippet, im getting the correct data. </p>

<pre><code>BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(file1.getAbsolutePath(), true);
Bitmap bitmap1 = bitmapRegionDecoder.decodeRegion(new Rect(top,left,width,height),options);
ByteBuffer buffer  = ByteBuffer.allocate(width*height*4);
  bitmapScaled.copyPixelsToBuffer(buffer);

  bitmapScaled.recycle();

File file2 = new File(Environment.getExternalStorageDirectory(),"regionFile");

  FileOutputStream out = new FileOutputStream(file2.getAbsolutePath());
  out.write(buffer.array());
</code></pre>

<p>If i use this snippet, i'm getting the correct data. But the drawback of using BitmapRegionDecoder is memory Leakage. The application will lose 1.5 MB of memory everytime, it executes this API. This memory is not possible to get back to the application with GC also.</p>

<p>So can any body please help me how to copy the data from bitmap to another file without losing the data.. It will be very much helpful for me if any body respond to this.. </p>

<p>Thanks in advance</p>
