如何使用位图将图像分享到社交媒体?

How to share image to social media with bitmap?

我需要从我的 RecyclerAdapter 共享一个图像,因为该图像最初并不存在,即使用适配器加载到 Activity 中。如何将位图分享到社交媒体?每次我在我的应用程序中单击共享时,它都会显示 "No apps can perform this action"。

feedItemView.setFeedSocialShareClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Share to Social Media

                ImageView imageView = (ImageView) feedItemView.findViewById(R.id.image);
                Drawable mDrawable = imageView.getDrawable();
                Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

                String path = MediaStore.Images.Media.insertImage(context.getContentResolver(),
                        mBitmap, "Design", null);

                Uri uri = Uri.parse(path);

                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image");
                share.putExtra(Intent.EXTRA_STREAM, uri);
                share.putExtra(Intent.EXTRA_TEXT, "I found something cool!");
                context.startActivity(Intent.createChooser(share, "Share Your Design!"));
            }
        });

试试

 share.setType("image/*");

Kotlin中,如果你的SDK低于29: 哪里需要给你的文件地址!

val share = Intent(Intent.ACTION_SEND)
          share.type = "image/jpeg"
          share.putExtra(Intent.EXTRA_STREAM,Uri.parse(file.path) ) // or Uri.fromFile(file)
          share.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
          share.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION
          startActivity(Intent.createChooser(share, "Share Image"))

但是如果你想覆盖所有 SDK ,最好使用 FileProvider