title: App crashing when trying to save to SD Card
body: <p>I'm trying to make an app that averages colour and stuff, which I have. I've got my colour in an <code>ImageView</code> and have made it into a bitmap, which I want to save on the SD card. But when I press the button to try and make it do so, it crashes.</p>
  
  <p>I'm also hoping to give it its' own folder when saving, and a custom file name. (In an ideal world, I'd like one that increases by one each time, like <strong>Swatch_01.png</strong>, <strong>Swatch_02.png</strong>, etc.)</p>
  
  <p>If it's something simple then I might cry a little bit.</p>
  
  <p><strong>Edit:</strong> Thank you guys, it's fixed. It hadn't created the JAverager_Swatches folder and the imageView wasn't converting to bitmap correctly. I had a look at it after a good amount of sleep and managed to solve it.</p>
  
  <pre><code>package com.colours.javerager;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import android.app.ActionBar;
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.drawable.BitmapDrawable;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v4.app.NavUtils;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class AverageImageActivity extends Activity { 
  
  public static final int MEDIA_TYPE_IMAGE = 1;
  public TextView red;
  public TextView green;
  public TextView blue;
  public TextView hex;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_average_image);
      ActionBar actionBar = getActionBar();
      actionBar.hide(); //hides action bar        
  
      final int avgColour2 = getIntent().getExtras().getInt("totalvalue");
      ImageView imgtest = (ImageView)findViewById(R.id.averageColourGenerated);
  
      imgtest.setBackgroundColor(avgColour2);
  
      final int r = getIntent().getExtras().getInt("redValue");
      final int g = getIntent().getExtras().getInt("greenValue");
      final int b = getIntent().getExtras().getInt("blueValue");
      final String hx = getIntent().getExtras().getString("hexValue");
  
      red = (TextView)findViewById(R.id.redTxt);
      red.setText("R: " + r);
  
      green = (TextView)findViewById(R.id.greenTxt);
      green.setText("G: " + g);
  
      blue = (TextView)findViewById(R.id.blueTxt);
      blue.setText("B: " + b);
  
      hex = (TextView)findViewById(R.id.hexTxt);
      hex.setText("Hex: #" + hx);
  
  }
  
  public void clickSaveButton(View v){
  
      final int avgColour2 = getIntent().getExtras().getInt("totalvalue");
  
      ImageView imgtest = (ImageView)findViewById(R.id.averageColourGenerated);
  
      imgtest.setBackgroundColor(avgColour2);
  
      BitmapDrawable drawable = (BitmapDrawable) imgtest.getDrawable();
      final Bitmap bmpimg = drawable.getBitmap();
          //turns imageView into a bitmap to save it
  
      OutputStream outStream = null;
      File file = new File(Environment.getExternalStorageDirectory() + "/Javerager_Swatches/", "Swatch.PNG");
      try {
          outStream = new FileOutputStream(file);
              bmpimg.compress(Bitmap.CompressFormat.PNG, 100, outStream);
              outStream.flush();
              outStream.close();
     } catch (FileNotFoundException e) {
  
     } catch (IOException e) {
     }
  
  }
  
  public void clickHomeButton(View v){
      Intent intent = new Intent(this, MenuScreenActivity.class);
      startActivity(intent);  
  }
  </code></pre>
  
  <p>Edit: There's a lot of logcat stuff and it's red and angry so ;~;</p>
  
  <blockquote>
    <p>04-19 23:30:54.308: E/AndroidRuntime(1184): FATAL EXCEPTION: main
    <p>04-19 23:30:54.308: E/AndroidRuntime(1184): Process:
    com.colours.javerager, PID: 1184 <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184): java.lang.IllegalStateException: Could not
    execute method of the activity <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   at
    android.view.View$1.onClick(View.java:3814) <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   at
    android.view.View.performClick(View.java:4424) <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   at
    android.view.View$PerformClick.run(View.java:18383) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    android.os.Handler.handleCallback(Handler.java:733) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    android.os.Handler.dispatchMessage(Handler.java:95) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    android.os.Looper.loop(Looper.java:137) <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   at
    android.app.ActivityThread.main(ActivityThread.java:4998) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    java.lang.reflect.Method.invokeNative(Native Method) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    java.lang.reflect.Method.invoke(Method.java:515) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    <p>04-19 23:30:54.308: E/AndroidRuntime(1184):    at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    dalvik.system.NativeStart.main(Native Method) <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184): Caused by:
    java.lang.reflect.InvocationTargetException <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   at
    java.lang.reflect.Method.invokeNative(Native Method) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    java.lang.reflect.Method.invoke(Method.java:515) <p>04-19
    23:30:54.308: E/AndroidRuntime(1184):     at
    android.view.View$1.onClick(View.java:3809) <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184):   ... 11 more <p>04-19 23:30:54.308:
    E/AndroidRuntime(1184): Caused by: java.lang.NullPointerException
    <p>04-19 23:30:54.308: E/AndroidRuntime(1184):    at
    com.colours.javerager.AverageImageActivity.clickSaveButton(AverageImageActivity.java:81)
    <p>04-19 23:30:54.308: E/AndroidRuntime(1184):    ... 14 more</p>
  </blockquote>