我正在为 Pinterest 做一个 android 共享 Intent ,但没有完全工作。我可以附加图片,但无法将文本发送到共享窗口中的“描述”字段。我尝试了不同的类型(text/plain、image/*、image/png),也尝试了 ACTION_SEND_MULTIPLE Intent 类型,但仍然没有成功。 Google chrome share intent 完美运行,所以我确信 Pinterest 支持此功能。这是我的代码:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, text);
if(file != null) intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setClassName(packageName, name);
this.startActivity(intent);
有什么想法吗?谢谢!
最佳答案
在 Pinterest SDK 的帮助下,我找到了一种通过普通 Android Intent 分享到 Pinterest 的方法(不使用 Pin It button developer docs ) .
基本上,您只需使用 Intent.ACTION_VIEW
打开这样的 URL;官方 Pinterest 应用程序支持这些 URL。 (我之前对 sharing to Twitter 使用了非常相似的方法。)
https://www.pinterest.com/pin/create/button/
?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F
&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg
&description=Next%20stop%3A%20Pinterest
为了更流畅的用户体验,set直接在 Pinterest 应用程序中打开的 Intent (如果已安装)。
一个完整的例子:
String shareUrl = "https://stackoverflow.com/questions/27388056/";
String mediaUrl = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
String description = "Pinterest sharing using Android intents"
String url = String.format(
"https://www.pinterest.com/pin/create/button/?url=%s&media=%s&description=%s",
urlEncode(shareUrl), urlEncode(mediaUrl), urlEncode(description));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
filterByPackageName(context, intent, "com.pinterest");
context.startActivity(intent);
上面使用的Util方法:
public static void filterByPackageName(Context context, Intent intent, String prefix) {
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
intent.setPackage(info.activityInfo.packageName);
return;
}
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
}
catch (UnsupportedEncodingException e) {
Log.wtf("", "UTF-8 should always be supported", e);
return "";
}
}
这是在安装了 Pinterest 应用程序的 Nexus 5 上的结果:
如果 未 安装 Pinterest 应用程序,也可以通过浏览器进行共享:
关于Pinterest 的 Android 分享 Intent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27388056/