在android中与whatsapp共享图像 [英] sharing image with whatsapp in android
问题描述
我有资产文件夹中的图像,需要与whatsapp应用程序共享
I have image in assets folder and need to share it with whatsapp application
我试过这段代码,它一直让我分享失败再试一次!怎么回事?!
I tried this code , it keeps give me sharing failed try again ! what's wrong ?!
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
// share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("file:///assets/epic/adv.png")));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
this.startActivity(Intent.createChooser(share, "share_via"));
推荐答案
您有几个问题。
首先, file:/// assets /
不是有效的 Uri
on任何版本的Android。您自己的应用程序可以通过 file:/// android_asset /
引用自己的资产。
First, file:///assets/
is not a valid Uri
on any version of Android. Your own application can refer to its own assets via file:///android_asset/
.
其次,只有你可以通过 file:/// android_asset /
访问您自己的资产 - 您无法将这样的 Uri
传递给第三位 - 派对应用。将文件从资产复制到内部存储并使用 FileProvider
,或者您可以尝试我的 StreamProvider
并尝试直接从 assets /
中分享数据。
Second, only you can access your own assets via file:///android_asset/
-- you cannot pass such a Uri
to third-party apps. Either copy the file from assets into internal storage and use FileProvider
, or you can try my StreamProvider
and try to share the data straight out of assets/
.
第三,无法保证设备上存在 com.whatsapp
,或 com.whatsapp
将支持 ACTION_SEND
file:///
Uri
MIME类型为 image / *
的值,因此您可能会因 ActivityNotFoundException $ c而崩溃$ c>。
Third, there is no guarantee that com.whatsapp
exists on the device, or that com.whatsapp
will support ACTION_SEND
of file:///
Uri
values with a MIME type of image/*
, and so you may crash with an ActivityNotFoundException
.
第四,用户可能希望通过WhatsApp以外的其他方式分享此图像。 请允许用户分享的位置用户想要,从 Intent
中删除 setPackage()
调用。
Fourth, the user might want to share this image via some other means than WhatsApp. Please allow the user to share where the user wants, by removing the setPackage()
call from your Intent
.
这篇关于在android中与whatsapp共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!