【问题标题】:Android share intent for Pinterest not workingPinterest 的 Android 共享意图不起作用
【发布时间】:2015-02-07 21:11:33
【问题描述】:

我正在为 Pinterest 做一个 android 共享意图,但没有完全工作。我可以附加图像,但无法将文本发送到共享窗口中的“描述”字段。我尝试了不同的类型(text/plain、image/*、image/png),还尝试了 ACTION_SEND_MULTIPLE 意图类型,但仍然没有运气。 Google chrome 共享意图完美运行,因此我确信 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);

有什么想法吗?谢谢!

【问题讨论】:

    标签: android android-intent pinterest


    【解决方案1】:

    在Pin It button developer docs 的帮助下,我找到了一种以简单的 Android 意图(不使用 Pinterest SDK)分享到 Pinterest 的方法。

    基本上,您只需使用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 应用程序中打开(如果已安装)。

    一个完整的例子:

    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 应用,通过浏览器也可以进行分享:

    【讨论】:

    • 你有没有想过用上面的方法分享本地图片。我很难弄清楚如何发送图像的 URL(图钉中的网站字段)以及图像和描述。这是我的相关问题 - stackoverflow.com/questions/35561031/…
    • 是否可以使用此方法从 SD 卡共享图像?如果我将mediaUrl 更改为我的文件路径,那么 pinterest 会显示通知,图像保存失败。
    【解决方案2】:

    由于某种原因,pinterest 应用程序不符合标准 (Intent.EXTRA_TEXT),所以我们必须单独添加它

    if(appInfo.activityInfo.packageName.contains("com.pinterest"){
            shareIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION","your description");
    }
    

    【讨论】:

    • 这似乎不再适用于 Android 4.4 及更早版本。有其他人看到这个问题吗?
    【解决方案3】:
    File imageFileToShare = new File(orgimagefilePath);
    
    Uri uri = Uri.fromFile(imageFileToShare);
    
    Intent sharePintrestIntent = new Intent(Intent.ACTION_SEND);
    sharePintrestIntent.setPackage("com.pinterest");
    sharePintrestIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION", text);
    sharePintrestIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sharePintrestIntent.setType("image/*");
    startActivityForResult(sharePintrestIntent, PINTEREST);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多