Bitmap Scaling crashes application


Last Updated:

  1. fat-wednesday

    fat-wednesday New Member This Topic's Starter

    Joined:
    Mar 14, 2010
    Messages:
    1
    Likes Received:
    0
    Hey everyone, first time posting here.

    Im working on an android app for a project at university and ive come across a bit of a bugger. I have the application loading images from the gallery (buggy but thats another issue), and then lets them draw over that image.

    The problem i have is trying to rescale the bitmap so that it fits onto the screen. After a bit of trial an error i found the only place to load the bitmap properly without it crashing the app (or just not displaying it at all) was in the onSizeChanged function which now looks like this.


    Code (Text):
    1. @Override
    2.             protected void onSizeChanged(int w, int h, int oldw,int oldh)
    3.             {
    4.                 int curW = mBitmap != null ? mBitmap.getWidth() : 0;
    5.                 int curH = mBitmap != null ? mBitmap.getHeight() : 0;
    6.                 if (curW >= w && curH >= h) return;
    7.                 if (curW < w) curW = w;
    8.                 if (curH < h) curH = h;
    9.  
    10.                 BitmapFactory.Options bfo = new BitmapFactory.Options();
    11.                 bfo.inSampleSize = 2;
    12.                 mBitmap = BitmapFactory.decodeFile(mFilename, bfo);
    13.                 bmpHeight = mBitmap.getHeight();
    14.                 bmpWidth = mBitmap.getWidth();
    15.                 if((bmpWidth>bmpHeight)&&(bmpWidth>screenWidth))
    16.                 {
    17.                     double ratio = screenWidth/bmpWidth;
    18.                     bmpWidth=(int)screenWidth;
    19.                     bmpHeight=(int)(ratio*bmpHeight);
    20.                 }
    21.                 else if((bmpHeight>bmpWidth)&&(bmpHeight>screenHeight))
    22.                 {
    23.                     double ratio = screenHeight/bmpHeight;
    24.                     bmpHeight=(int)screenHeight;
    25.                     bmpWidth=(int)(ratio*bmpWidth);
    26.                 }
    27.                
    28.                 //mBitmap = Bitmap.createScaledBitmap(mBitmap, bmpWidth, bmpHeight, true);
    29.                 //Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, bmpWidth, bmpHeight, true);
    30.                
    31.                 Bitmap newBitmap = Bitmap.createBitmap(curW, curH,Bitmap.Config.RGB_565);
    32.                
    33.                 Canvas newCanvas = new Canvas();
    34.                 newCanvas.setBitmap(newBitmap);
    35.                 if (mBitmap != null) {
    36.                     newCanvas.drawBitmap(mBitmap, 0, 0, null);
    37.                 }
    38.                 mBitmap = newBitmap;
    39.                 mCanvas = newCanvas;
    40.             }
    The code as it is here draws the bitmap image to the canvas, but doesn't scale it. The two commented out lines are things i tried to get the image to scale. neither worked.

    any ideas would be very much appreciated.
     

    Advertisement

Share This Page

Loading...