title: Share Image From Viewpager
body: <p>I want to share the current item from a ViewPager but I cannot get the current image and share it.</p>
  
  <p>The share button is only on promotion page's action bar, and it will share the current item from the ViewPager.</p>
  
  <p>This is my adapter:</p>
  
  <pre><code>class CustomPagerAdapter extends PagerAdapter {
  int[] mResources = {
          R.drawable.abc1,
          R.drawable.abc2
  };
  Context mContext;
  LayoutInflater mLayoutInflater;
  
  public CustomPagerAdapter(Context context) {
      mContext = context;
      mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  @Override
  public int getCount() {
      return mResources.length;
  }
  
  @Override
  public boolean isViewFromObject(View view, Object object) {
      return view == ((LinearLayout) object);
  }
  
  @Override
  public Object instantiateItem(ViewGroup container, int position) {
      View itemView = mLayoutInflater.inflate(R.layout.slidingimages_layout, container, false);
  
      ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
      imageView.setImageResource(mResources[position]);
  
      container.addView(itemView);
      return itemView;
  }
  
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((LinearLayout) object);
  }
  }
  </code></pre>
  
  <p>And this is my Fragment Java code:</p>
  
  <pre><code>public class PromotionFragment extends Fragment {
  Menu menu;
  private PopupWindow Pro_popup;
  
  public PromotionFragment() {
      // Required empty public constructor
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);
  }
  public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
      // Inflate the menu; this adds items to the action bar if it is present.
      inflater.inflate(R.menu.toolbar_menu, menu);//Menu Resource, Menu
      super.onCreateOptionsMenu(menu,inflater);
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
  
      View view = inflater.inflate(R.layout.fragment_promotion, container, false);
      CustomPagerAdapter adapter = new CustomPagerAdapter(getActivity());
  
      ViewPager viewpager = (ViewPager) view.findViewById(R.id.pager);
      viewpager.setAdapter(adapter);
  
      LinePageIndicator titleIndicator = (LinePageIndicator)view.findViewById(R.id.indicator);
      titleIndicator.setViewPager(viewpager);
  
  
      return view;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      Display display = getActivity().getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      final int width = size.x;
      final int height = size.y;
      RelativeLayout pro_popup = (RelativeLayout) getActivity().findViewById(R.id.pro_popup);
      LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
      View pro_popupview = inflater.inflate(R.layout.promotion_popup, null);
      TextView qr_title = (TextView) pro_popupview.findViewById(R.id.qr_title);
      TextView qr_con = (TextView) pro_popupview.findViewById(R.id.qr_con);
      RelativeLayout popupbg2 = (RelativeLayout) pro_popupview.findViewById(R.id.prepopup);
      Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_code);
      Button scan_btn = (Button) pro_popupview.findViewById(R.id.scan_btn);
      scan_btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
          }
      });
      Button dismiss = (Button) pro_popupview.findViewById(R.id.propop_dismiss);
      dismiss.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Pro_popup.dismiss();
          }
  
      });
      switch (item.getItemId()) {
          case R.id.qr_scan:
              qr_title.setText(R.string.propop_title);
              qr_con.setText(R.string.propop_con);
              Pro_popup = new PopupWindow(pro_popupview);
              Pro_popup.setWidth(width - 80);
              Pro_popup.setHeight(height - 400);
              Pro_popup.setOutsideTouchable(true);
              Pro_popup.setFocusable(true);
              Pro_popup.showAtLocation(pro_popup, Gravity.CENTER, 0, -60);
  
              return true;
          case R.id.toolbarshare:
              final Intent shareIntent = new Intent(Intent.ACTION_SEND);
              shareIntent.setType("image/jpg");
              startActivity(Intent.createChooser(shareIntent, "Share image using"));
              CustomPagerAdapter adapter = new CustomPagerAdapter(getActivity());
              ViewPager viewpager = (ViewPager)getActivity().findViewById(R.id.pager);
              viewpager.setAdapter(adapter);
              int position = viewpager.getCurrentItem();
              if(position == 0)
              {
                  final File photoFile = new File(getActivity().getFilesDir(), "abc1.png");
                  shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
              }
              else
              {
                  final File photoFile = new File(getActivity().getFilesDir(), "abc1.png");
                  shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
              }
              return true;
      }
  
      return false;
  }
  </code></pre>