0

I am making a drawing app such that user can choose to load pictures from the gallery and further draw on it. However, if the size of the photo is greater than the screen, it can only show part of the picture.

   public void load_pic(String picturePath) // load a picture from gallery
   {
      bitmap = (BitmapFactory.decodeFile(picturePath)).copy(Bitmap.Config.ARGB_8888, true);
      bitmapCanvas = new Canvas(bitmap);
      invalidate();
   }

How could i code such that the picture can either

  1. load in such that can fit either maximium allowable screen width or height, or
  2. load in such that can pull the image to occupy the full screen

Also, running on some device it would give out java.lang.OutOfMemoryError...and it crashed...How could that be tackled?

Many thanks in advance!!

pearmak
  • 4,979
  • 15
  • 64
  • 122

1 Answers1

0

First you need to find the screen width and height which you can accomplish by using below code.

Display display = getWindowManager().getDefaultDisplay();
int newWidth = display.getWidth();
int newHeight = display.getHeight();

If you are not in activity you can get the display as describe below,

WindowManager wm = (WindowManager) mContex.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

You can scale the bitmap using below code.

Bitmap scaled = Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true);

You can also set the height and width at the time of the decoding using, outWidth and outHeight attribute of the BitmapFactory.Options.

And to overcome the outOfMemoryError you can refer the answer pointed in comment.

P.S. I wrote that linked answer.

Hope this will help you.

Community
  • 1
  • 1
Moin Ahmed
  • 2,898
  • 21
  • 33