I am making Image gallery app in which I retrieve images from server using glide and show them on RecyclerView and when user click on RecyclerView item it will open DetailActivity in which clicked image will be shown and also all the available images till here everything is working fine now i added share functionality I am facing problem here it shares Next image not the Current. Suppose current image is image1 but it shares image2 not 1. I read all the related posts on SO the only thing I came to know is about getcurrentItem and nothing else. Need some advice to solve this! Thanks in Advance!

Problem:- It Shares Next Image not the Current Image


protected void onCreate(Bundle savedInstanceState) {

        viewPager = findViewById(R.id.viewPager);
        ImageView imageView = findViewById(R.id.share);
        final String fileName = getIntent().getStringExtra("filename");
        position = getIntent().getIntExtra("pos",0); <- getting position frm recyclerview
        //Datum datum = (Datum) getIntent().getSerializableExtra("datum");
        //fileName = datum.getFileName();
        url = "url link";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            public void onResponse(String response) {
                Log.d("CODE", response);
                GsonBuilder gsonBuilder = new GsonBuilder();
                Gson gson = gsonBuilder.create();
                Image users =gson.fromJson(response, Image.class);
                List<Trending> data = users.getData();
                //userlist.setAdapter(new GithubAdapter(MainActivity.this, data));
                adapter = new ViewPagerAdapter(context,data);
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SlideActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();

        RequestQueue queue = Volley.newRequestQueue(this);



public ViewPagerAdapter(Context context, List<Trending> data) {
        this.context = context;
        this.data = data;

    public Object instantiateItem(@NonNull final ViewGroup container,int position) {

        inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.pager_item, container, false);
        wallpaper = view.findViewById(R.id.walpaperImage);
        share = view.findViewById(R.id.share_img_trending_viewpager);
        //Datum datum = this.datum.getFileName();
        Trending datum = data.get(position);
        final String fileName = datum.getFileName();
        url = "url link" + fileName;

    share.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                        //ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT);
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 00);
                   else {

         container.addView(view, 0);
        return view;

shareitem code which i declared out of instantiateItem:

public void onShareItem(View v) {
        // Get access to bitmap image from view
        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(wallpaper);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            // Launch sharing dialog for image
            context.startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
    public Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable){
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
            return null;
        // Store image to default external storage directory
        Uri bmpUri = null;
        try {
            // Use methods on Context to access package-specific directories on external storage.
            // This way, you don't need to request external read/write permission.
            // See https://youtu.be/5xVh-7ywKpE?t=25m25s
            File file =  new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            // **Warning:** This will fail for API >= 24, use a FileProvider as shown below instead.
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
        return bmpUri;

0 Answers