Work with large images in android

I have an Android app in which I work with really large images (640x480) and a bit more. These are actually pictures taken with the camera, then edited, then saved to sdcard and finally uploaded to the server. But the problem I ran into is VM memory exceeded while working with bitmaps .

I am doing something like this:

In the first step, I get the bytes from the camera and create a bitmap that is edited and then saved to sdcard

  BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; options.inDither = true; byte[] imageData = extras.getByteArray("imageData"); myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options); Matrix mat = new Matrix(); mat.postRotate(90); if(myImage !=null){ bitmapResult = Bitmap.createBitmap(myImage.get(), 0, 0, (myImage.get()).getWidth(), (myImage.get()).getHeight(), mat, true); 

in onPause() I did this:

  bitmapResult.recycle(); bitmapResult=null; 

In my second step, I read the file from sdcard and show it on the screen. Why? I have my reasons:

 File f=new File("/sdcard/Images/android.jpg"); BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inTempStorage = new byte[16*1024]; o2.inSampleSize=8; o2.outWidth=100; o2.outHeight=100; try { x = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(x != null){ myImage.setImageBitmap(x); } 

And did the same in onPause()

 x.recycle(); x=null; 

All this did not work and after a few shots my application crashed.

I tried using WeaakReference instead of bitmap :

 myImage = new WeakReference<Bitmap>(BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options)); 

And yet nothing .... the same error .... out of memory .

Does anyone have any ideas?

PS: I also tried calling System.gc() , but with the same result!

So please help me!

+4
source share
1 answer

Use the code below to resize the image.

`

 Bitmap bmp = BitmapFactory.decodeFile(imageFilePath); int width = bmp.getWidth(); int height = bmp.getHeight(); float scaleWidth = ((float) 300) / width; float scaleHeight = ((float) 300) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); ByteArrayOutputStream baostream = new ByteArrayOutputStream(); resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baostream); 

`

0
source

All Articles