通过分享意图分享图像到 Whatsapp
Posted
技术标签:
【中文标题】通过分享意图分享图像到 Whatsapp【英文标题】:Share image through share intent to Whatsapp 【发布时间】:2021-11-14 21:51:14 【问题描述】:这是我通过它将我的图像或视频从适配器共享到 WhatsApp 的代码,它工作正常,但现在只显示吐司没有安装什么应用程序,代码中有任何问题我错过了什么吗?
public void shareWhatsapp(String type, String path, String package_name)
Uri uri = FileProvider.getUriForFile(mFragment, BuildConfig.APPLICATION_ID + ".provider", new File(path));
PackageManager pm = mFragment.getPackageManager();
try
PackageInfo info = pm.getPackageInfo(package_name, PackageManager.GET_META_DATA);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.setType(type);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setPackage(package_name);
mFragment.startActivity(Intent.createChooser(sharingIntent, "Share via"));
catch (PackageManager.NameNotFoundException e)
Toast.makeText(mFragment, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
这里是听众
holder.repostWhatsapp.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
final ModelStatus curVideo = getItem(position);
if (curVideo.getFull_path().endsWith(".jpg"))
shareWhatsapp("image/jpg", curVideo.getFull_path(), "com.whatsapp");
else if (curVideo.getFull_path().endsWith(".mp4"))
shareWhatsapp("video/mp4", curVideo.getFull_path(), "com.whatsapp");
);
【问题讨论】:
【参考方案1】:使用以下代码将图像或视频分享到whatsapp
holder.repostWhatsapp.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this,
PdfRendererActivity.this.getPackageName() + ".provider", outputFile);
if (curVideo.getFull_path().endsWith(".jpg"))
shareWhatsapp("image/jpg", uri);
else if (curVideo.getFull_path().endsWith(".mp4"))
shareWhatsapp("video/mp4", uri);
);
分享WhatsApp
void shareWhatsapp(String type, String uri)
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");
startActivity(share);
【讨论】:
我还有一个关于 facebook 和 Instagram 的问题 if (curVideo.getFull_path().endsWith(".jpg")) shareInstagram("image/jpg", curVideo.getFull_path(), "com.instagram.android"); else if (curVideo.getFull_path().endsWith(".mp4")) shareInstagram("video/mp4", curVideo.getFull_path(), "com.instagram.android"); 相同的代码,但包是为 facebook 的 com.facebook.katana 如果对你有帮助别忘了标记接受以上是关于通过分享意图分享图像到 Whatsapp的主要内容,如果未能解决你的问题,请参考以下文章