无法共享ViewPager中的当前图像?为什么共享下一张图片?

时间:2019-04-27 06:44:04

标签: android android-viewpager

我正在制作图像库应用程序,在其中我可以使用滑行从服务器检索图像并将其显示在RecyclerView上,当用户单击RecyclerView项目时,它将打开DetailActivity,在其中将显示单击的图像以及所有可用的图像,直到此处一切现在工作正常,我添加了共享功能,我在这里遇到问题,它共享下一个图像而不是当前图像。假设当前图像是image1,但它共享的是image2,而不是1。需要一些建议来解决这个问题!预先感谢!

问题:-它共享下一个图像而不是当前图像

SlideActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_activity);

        viewPager = findViewById(R.id.viewPager);
        ImageView imageView = findViewById(R.id.share);
        //getIntentInfo();
        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";
        //Toast.makeText(context,url,Toast.LENGTH_SHORT).show();
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            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);
                viewPager.setAdapter(adapter);
                viewPager.setCurrentItem(position);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SlideActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            }
        });

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);

    }

ViewPagerAdapter

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

    @SuppressLint("ClickableViewAccessibility")
    @NonNull
    @Override
    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;
        Glide.with(context).load(url).into(wallpaper);

    share.setOnClickListener(new View.OnClickListener() {
                @Override
                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 {

                        onShareItem(wallpaper);
                    }
                }
            });
         container.addView(view, 0);
        return view;
}
我在InstantiateItem中声明的

shareitem代码:

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.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.setType("image/*");
            // 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);
            out.close();
            // **Warning:** This will fail for API >= 24, use a FileProvider as shown below instead.
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

0 个答案:

没有答案