我想在我的应用程序中将图像从Firebase共享到Whatsapp,Facebook等。
如果点击它,我想要分享图片。
这是我的ViewHolder:
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView= itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView content = (ImageView)mView.findViewById(R.id.imageViewy);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
v.getContext().startActivity(Intent.createChooser(share,"Share via"));
}
});
}
首先它正在工作,如果我点击图像我可以选择我想要分享图像的位置。然后,如果我选择Whatsapp,它会说“分享失败,请再试一次”
我做错了吗?请告诉我如何解决这个问题。
我也试过这段代码:
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView= itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView content = (ImageView)mView.findViewById(R.id.imageViewy);
content.setDrawingCacheEnabled(true);
Bitmap icon = content.getDrawingCache();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
v.getContext().startActivity(Intent.createChooser(share, "Share Image"));
}
});
}
答案 0 :(得分:0)
ImageView content = (ImageView)mView.findViewById(R.id.imageViewy);
content.setDrawingCacheEnabled(true);
Uri imageUri= Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), content, "title", "discription"));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
如本文档建议https://developer.android.com/training/sharing/send所示, MediaStore适合共享内容。
请注意,将内容添加到系统MediaStore后,设备上的任何应用程序都可以访问该内容。
当然,您还需要存储权限。