在 Instagram 上分享并获得回调

Share in Instagram and get callback

我必须将应用程序中的图片分享到 instagram。我浏览了 Instagram 开发者文档,那里他们只提到通过 Intent 方法分享,但我需要的是我必须在成功发布后获得回调。

private void createInstagramIntent(String type, String mediaPath){

    // Create the new Intent using the 'Send' action.
    Intent share = new Intent(Intent.ACTION_SEND);

    // Set the MIME type
    share.setType(type);

    // Create the URI from the media
    File media = new File(mediaPath);
    Uri uri = Uri.fromFile(media);

    // Add the URI to the Intent.
    share.putExtra(Intent.EXTRA_STREAM, uri);

    // Broadcast the Intent.
    startActivity(Intent.createChooser(share, "Share to"));
}

有什么办法吗?

根据API,有多种选择。首先参考他们的 Android Intents, that is a bad example IMHO, this is more or less the default way in Android how to share data. What you want is something more like to open just their app. For such cases is there the setPackage() 选项:

// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);

// Limit this call to instagram
share.setPackage("com.instagram.android");

// Set the MIME type
share.setType(type);

// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);

// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);

try {
    // Fire the Intent.
    startActivityForResult(share, REQUEST_CODE);
} catch(ActivityNotFoundException e) {
    // instagram not installed
}

现在,对于没有安装 instagram 的情况,你几乎迷路了。如果没有其他的SDK,你什么也做不了。

一个非常复杂的解决方法可能是将文件上传到您自己的后端。然后打开浏览器以访问 oauth 令牌以通​​过后端上传图像。然而,这是一个我会避免的安静昂贵的解决方案。