title: How to save image in view pager android?
body: <p>I am using a GridView where images (as Drawables) will be clicked to display in a ViewPager (for swiping between the images). I am using a save button to save an image to the SD card, but I am having trouble in doing so.</p>
  
  <p>Here is the code:</p>
  
  <p>ViewPagerImage:</p>
  
  <pre><code>public class ViewPagerImage extends Activity {
      // Declare Variable
      int position;
      Button save;
      Context mContext ;
  
      final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Images/");
      boolean success = false;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Set title for the ViewPager
          setTitle("ViewPager");
          // Get the view from view_pager.xml
          setContentView(R.layout.viewpager);
          save=(Button)findViewById(R.id.button1);
  
          // Retrieve data from MainActivity on item click event
          Intent p = getIntent();
          position = p.getExtras().getInt("id");
  
          final ImageAdapter imageAdapter = new ImageAdapter(this);
          List&lt;ImageView&gt; images = new ArrayList&lt;ImageView&gt;();
  
          // Retrieve all the images
          for (int i = 0; i &lt; imageAdapter.getCount(); i++) {
              ImageView imageView = new ImageView(this);
              imageView.setImageResource(imageAdapter.mThumbIds[i]);
              imageView.setScaleType(ImageView.ScaleType.CENTER);
              images.add(imageView);
          }
  
          // Set the images into ViewPager
          final ImageViewPagerAdapter pageradapter = new ImageViewPagerAdapter(images);
          final ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
          viewpager.setAdapter(pageradapter);
          // Show images following the position
          viewpager.setCurrentItem(position);
          save.setOnClickListener(new View.OnClickListener() {
  
                  @Override
                  public void onClick(View arg0) {
                  // TODO Auto-generated method stub
  
                  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);
                  //I don't know what to use in this.. :(
                  //Drawable drawable = mContext.getResources().getDrawable();
                  Bitmap bitmap =((BitmapDrawable) 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())));
  
                  }
          });
      }
      // ...
  }
  </code></pre>
  
  <p>ImageViewPagerAdapter:</p>
  
  <pre><code>public class ImageViewPagerAdapter extends PagerAdapter {
      private List&lt;ImageView&gt; images;
  
      public ImageViewPagerAdapter(List&lt;ImageView&gt; images) {
          this.images = images;
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          ImageView imageView = images.get(position);
          container.addView(imageView);
          return imageView;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          container.removeView(images.get(position));
      }
  
      @Override
      public int getCount() {
          return images.size();
      }
  
      @Override
      public boolean isViewFromObject(View view, Object o) {
          return view == o;
      }
  }
  </code></pre>