Android share intent for Pinterest not working

后端 未结 3 667
清酒与你
清酒与你 2020-12-07 00:23

I am doing an android share intent for Pinterest but is not fully working. I am able to attach the image but I can\'t send text to the \"description\" field in the share win

相关标签:
3条回答
  • 2020-12-07 00:30
    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);
    
    0 讨论(0)
  • 2020-12-07 00:32

    I found a way to share to Pinterest with plain Android intents (without using the Pinterest SDK), with help from Pin It button developer docs.

    Basically you just open an URL like this with Intent.ACTION_VIEW; the official Pinterest app kindly supports these URLs. (I've earlier used a very similar approach for 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
    

    And for smoother user experience, set the intent to open directly in Pinterest app, if installed.

    A complete example:

    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 methods used above:

    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 "";
        }
    }
    

    This is the result on a Nexus 5 with Pinterest app installed:

    And if Pinterest app is not installed, sharing works just fine via a browser too:

    0 讨论(0)
  • 2020-12-07 00:57

    for some reason pinterest app doesn't comply to the standard (Intent.EXTRA_TEXT) so we have to add it separately

    if(appInfo.activityInfo.packageName.contains("com.pinterest"){
            shareIntent.putExtra("com.pinterest.EXTRA_DESCRIPTION","your description");
    }
    
    0 讨论(0)
提交回复
热议问题