title: How to save image from ViewPager?
body: <p>I have an activity with ViewPager. I'm using it to swipe images. I also have a save button, and I need to save the current image to SD card. But I get an error. Here's my XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/rl_view_pager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clickable="true"
  android:focusable="true"
  android:focusableInTouchMode="true" &gt;
  
  &lt;android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /&gt;
  
    &lt;Button
      android:id="@+id/bSave"
      android:layout_width="80dp"
      android:layout_height="40dp"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="15dp"
      android:layout_marginTop="15dp"
      android:text="Save"
      android:layout_alignParentLeft="true"
      android:textSize="24sp"
      android:padding="0dp"
      android:background="@drawable/buttons" /&gt;
  
    &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And here's my class:</p>
  
  <pre><code>public class Photo_gallery extends Activity implements OnClickListener{
  
      Button save;
  
      final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Images/");
      boolean success = false;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.photo_gallery);
  
          save = (Button) findViewById(R.id.bSave);
  
          final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
          ImagePagerAdapter adapter = new ImagePagerAdapter();
          viewPager.setAdapter(adapter);
  
          save.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  final Random generator = new Random();
                  int n = 10000;
                  n = generator.nextInt(n);
                  final String fname = "StyleMe-" + n + ".png";
  
                  myDir.mkdirs();
  
                  File image = new File(myDir, fname);
  
                  BitmapDrawable drawable = (BitmapDrawable) viewPager.getBackground();
                  Bitmap bitmap = drawable.getBitmap();
  
                  // Encode the file as a PNG image.
                  FileOutputStream outStream;
                  try {
  
                      outStream = new FileOutputStream(image);
                      bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
                      /* 100 to keep full quality of the image */
  
                      outStream.flush();
                      outStream.close();
                      success = true;
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
                  if (success) {
                      Toast.makeText(getApplicationContext(), "Image saved with success at /sdcard/Pictures/SexyImages",
                              Toast.LENGTH_LONG).show();
                  } else {
                      Toast.makeText(getApplicationContext(),
                              "Error during image saving", Toast.LENGTH_LONG).show();
                  }
                  sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
                          ("file://"
                                                  + Environment.getExternalStorageDirectory())));
  
              }
          });
  
      }
  
      private class ImagePagerAdapter extends PagerAdapter {
          private int[] mImages = new int[] {
              R.drawable.p1,
              R.drawable.p2,
              R.drawable.p3,
            .
            .
            .
            .
              R.drawable.p108
          };
  
          @Override
          public int getCount() {
            return mImages.length;
          }
  
          @Override
          public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
          }
  
          @Override
          public Object instantiateItem(ViewGroup container, int position) {
            Context context = Photo_gallery.this;
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(
                R.dimen.padding_medium);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
          }
  
          @Override
          public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
          }
        }
  
      public void onClick(View arg0) {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p>I get error on this line (java.lang.NullPointerException):</p>
  
  <pre><code>Bitmap bitmap = drawable.getBitmap();
  </code></pre>