Android share intent for Pinterest not working

江枫思渺然 提交于 2019-12-17 16:54:04

问题


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 window. I've tried different types (text/plain, image/*, image/png) and also tried the ACTION_SEND_MULTIPLE intent type but still no luck. Google chrome share intent works perfectly so I'm sure Pinterest supports this functionality. Here is my code:

    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);

Any idea? thanks!


回答1:


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:




回答2:


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



回答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);


来源:https://stackoverflow.com/questions/27388056/android-share-intent-for-pinterest-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!