title: resizing the canvas dimensions and size of a bitmap file
body: <p>My application is attempting to resize a PNG file from external storage that is rectangular: <em>(1000 w x 2000 h for example)</em></p>
  
  <p>The goal is to turn it into a a 256 x 256 image, by adding blank space on both sides of the image so that it is now 2000 x 2000 and then scale it down to 256x256 thus making it square without stretching the original image.  </p>
  
  <p>I thought i could be clever and use <code>ImageView</code> to do this and add a preview of the image scaled to the screen but i am crashing with a nullpointer.... </p>
  
  <p><strong>My resize method</strong></p>
  
  <pre><code>private void  resizeImage(String fileName){
          ImageView tempImageView = new ImageView(this);
          tempImageView.setDrawingCacheEnabled(true);
          tempImageView.setLayoutParams(new LayoutParams(256, 256));
          tempImageView.setScaleType(ScaleType.CENTER_INSIDE);
          Bitmap bm = BitmapFactory.decodeFile(getKingNozzleStorageDirectory()+fileName);
          tempImageView.setImageBitmap(bm);
          rootView.addView(tempImageView);
          tempImageView.buildDrawingCache();
          Bitmap resized_bm = tempImageView.getDrawingCache();
  
          FileOutputStream outStream;
  
          try {
              outStream = new FileOutputStream(getKingNozzleStorageDirectory() + "new"+fileName );
  
              try {
                  resized_bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                  outStream.flush();
                  outStream.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
      }
  </code></pre>
  
  <p><strong>The Exception i am catching:</strong></p>
  
  <blockquote>
    <p>06-03 08:43:50.006: W/System.err(3842): java.lang.NullPointerException
    06-03 08:43:50.006: W/System.err(3842):   at
    com.eliddell.kingnozzle.activities.KNDrawingActivity.resizeImage(KNDrawingActivity.java:285)
    06-03 08:43:50.006: W/System.err(3842):   at
    com.eliddell.kingnozzle.activities.KNDrawingActivity.onClick(KNDrawingActivity.java:223)
    06-03 08:43:50.006: W/System.err(3842):   at
    android.view.View.performClick(View.java:4438) 06-03 08:43:50.006:
    W/System.err(3842):   at
    android.view.View$PerformClick.run(View.java:18422) 06-03
    08:43:50.006: W/System.err(3842):     at
    android.os.Handler.handleCallback(Handler.java:733) 06-03
    08:43:50.006: W/System.err(3842):     at
    android.os.Handler.dispatchMessage(Handler.java:95) 06-03
    08:43:50.006: W/System.err(3842):     at
    android.os.Looper.loop(Looper.java:136) 06-03 08:43:50.006:
    W/System.err(3842):   at
    android.app.ActivityThread.main(ActivityThread.java:5017) 06-03
    08:43:50.006: W/System.err(3842):     at
    java.lang.reflect.Method.invokeNative(Native Method) 06-03
    08:43:50.006: W/System.err(3842):     at
    java.lang.reflect.Method.invoke(Method.java:515) 06-03 08:43:50.006:
    W/System.err(3842):   at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    06-03 08:43:50.006: W/System.err(3842):   at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-03
    08:43:50.006: W/System.err(3842):     at
    dalvik.system.NativeStart.main(Native Method)</p>
  </blockquote>
  
  <p><strong>If i comment out the following line</strong></p>
  
  <pre><code>resized_bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
  </code></pre>
  
  <p>it doesn't crash which leads me to believe the resize_bm is not being created?</p>
  
  <pre><code>Bitmap resized_bm = tempImageView.getDrawingCache();
  </code></pre>
  
  <p>Obviously there are probably other ways of achieving my canvas resizing, so those insights are of course welcome.. but anyone know what i am doing wrong?</p>
  
  <p>I didn't see any native ways to add canvas padding to the width of a bitmap..</p>