-5

I tried to post an image on Facebook wall from my android application.For this I created a bitmap from layout.Here is my code

                    System.gc();
            Bitmap bitmap = Bitmap.createBitmap(
                    view.getMeasuredWidth(),
                    view.getMeasuredHeight(),
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Drawable bgDrawable = newLinearLayout.getBackground();
            if (bgDrawable != null)
                bgDrawable.draw(canvas);
            else
                canvas.drawColor(Color.WHITE);
            view.draw(canvas);
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(1.0f, 1.0f);

            // recreate the new Bitmap
            Bitmap resizedBitmap = null;
            resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            //Canvas resizedCanvas = new Canvas(resizedBitmap);

            if (bitmap != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                Bundle parameters = new Bundle();
                byte[] data = baos.toByteArray();
                parameters.putByteArray("picture", data);
                DataKeeper.getInstance().setBundleToPost(parameters);

            }
            bitmap.recycle();

It shows OutOfMemory exception.I know that this is due to imagesize exceeds its available size.How can i resize image without lossing its quality? I tried to correct this using BitmapFactory.Options. But does not work.How can solve this exception?

Regards Asha

Asha Soman
  • 35
  • 1
  • 5
  • possible duplicate of [Android: Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Ovidiu Latcu Aug 13 '12 at 10:49

3 Answers3

0

use this :

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

for more detail see this link

link

Community
  • 1
  • 1
Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
  • I want bitmap of my Layout content.In such a situation how can i use Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options); – Asha Soman Aug 13 '12 at 11:00
0

The idea of @Zaz Gmy is correct,
however I don't think the value of inSampleSize should be constant (8) for every image.

This of course will work, but scaling different images with different resolutions on the same scale factor = 8, will cause some of images to have a visible loss of quality.

Instead, the inSimpleSize should be generated based on the target width and height of image.

Android documentation provides a good article on how to deal with this issue.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • But this is another question, not specific to this thread. Anyway, take a look over this thread on how to save a Bitmap from your content: http://stackoverflow.com/questions/2174875/android-canvas-to-jpg – Andy Res Aug 13 '12 at 11:01
0

Please check this tutorial and download the sample project.This will solve your problem

http://developer.sonymobile.com/wp/2011/06/27/how-to-scale-images-for-your-android%E2%84%A2-application/

reji
  • 171
  • 1
  • 12