如何从RecyclerView共享图像?

时间:2016-11-14 16:22:37

标签: android android-recyclerview

我的目标是将 ImageView 内的图像 RecyclerView 分享给其他应用。使用Picasso lib从 Firebase存储加载图片

我在每个项目下方添加了一个“分享”按钮,但无法继续进行。

这是我的MainActivity。

public class MainActivity extends AppCompatActivity {

private RecyclerView mBlogList;

private DatabaseReference mDatabase;

private LinearLayoutManager mLayoutManager;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
    mDatabase.keepSynced(true);
    mBlogList = (RecyclerView) findViewById(R.id.blog_List);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(this));
    mLayoutManager = new LinearLayoutManager(MainActivity.this);
    mLayoutManager.setReverseLayout(true); // THIS ALSO SETS     setStackFromBottom to true
    mBlogList.setLayoutManager(mLayoutManager);
    mLayoutManager.setStackFromEnd(true);




}

@Override
protected void onStart() {
    super.onStart();

FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new            FirebaseRecyclerAdapter<Blog, BlogViewHolder>(

            Blog.class,
            R.layout.blog_row,
            BlogViewHolder.class,
            mDatabase
    ) {

        @Override

        protected void populateViewHolder(BlogViewHolder viewHolder, Blog model,int position) {
            viewHolder.setTitle(model.getTitle());

            viewHolder.setImage(getApplicationContext(), model.getImage());


        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);



}

public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;


    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }


    public void setTitle(String title) {
        TextView post_title = (TextView) mView.findViewById(R.id.post_title);
        post_title.setText(title);
    }

    public void setImage(final Context ctx, final String image) {
        final ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
        Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                //Try again online if cache failed
                Picasso.with(ctx)
                        .load(image)
                        .error(R.drawable.header)
                        .into(post_image, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError() {
                                Log.v("Picasso", "Could not fetch image");
                            }



                        });



            }
        });
    }



}

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码与可用选项(twitter,facebook,gmail,WhatsApp等等)共享:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image via:"));

我没有尝试过,但基本上是这样的。 Facebook可能不允许从其他应用程序共享。然后你可以使用facebook sdk(GraphApi)

修改

参阅官方文档

https://developer.android.com/training/sharing/index.html

答案 1 :(得分:1)

您需要先将imageView转换为Bitmap,然后再分享。

 // Can be triggered by a view event such as a button press
public void onShareItem(View v) {
    // Get access to bitmap image from view
    ImageView ivImage = (ImageView) findViewById(R.id.post_image);
    // Get access to the URI for the bitmap
    Uri bmpUri = getLocalBitmapUri(ivImage);
    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
        startActivity(Intent.createChooser(shareIntent, "Share Image"));

    } else {

    }
}

// Returns the URI path to the Bitmap displayed in specified ImageView
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 {
        File file =  new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

如何在视图中设置共享按钮。

    protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
            viewHolder.setTitle(model.getTitle());

            viewHolder.setImage(getApplicationContext(), model.getImage());


            viewHolder.mShareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    onShareItem(view);



                }
            });


        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);


}

public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;

    Button mShareButton;


    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mShareButton = (Button) mView.findViewById(R.id.btn_share);
    }