Reading Raw data from Bitmap files
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.
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());
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.
When i see the data in the "decodeFile" half of the data will becomes null, and the data is improper.
The above is one type of method of reading the raw data using Bitmap class methods.
When i follow the same thing by using the below code snippet, im getting the correct data.
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());
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 A开发者_StackOverflow社区PI. This memory is not possible to get back to the application with GC also.
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..
Thanks in advance
See my answer to my own question here
You'll be interest in the onActivityResult method.
精彩评论