我有一个recyclerView,它使用picasso从firebase加载图像。每个项目(图像)下面都有一个shareButton,点击它时将视图转换为位图并使用共享意图与其他应用程序共享。问题是它共享下一个图像(项目),而不是共享shareButton的图像。 以下是位图转换和分享的代码 -
// 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);
}
这就是我添加数据的方式
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);
}
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() {
Picasso.with(ctx)
.load(image)
.error(R.drawable.header)
.placeholder(R.drawable.progress_animation)
.into(post_image);
}
});
}
}
}
答案 0 :(得分:0)
为什么不将图像本身传递给共享方法?
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(model.getImage());
}
});
}
然后直接分享图像uri。
// Can be triggered by a view event such as a button press
public void onShareItem(String image) {
Uri imageUri = Uri.parse(image);
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}