Search code examples
androidcallbackinstagramsharinginstagram-api

Share in Instagram and get callback


I have to share an image from my application to instagram. I have gone through Instagram developer documentation and there they mentioning only about sharing via Intent method, but what I need is I have to get a callback after posting successfully.

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

is there any way to do this ?


Solution

  • There are several options depending on their API. At first referring their 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() option:

    // 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
    }
    

    Now for the case that instagram is not installed you are almost lost. If there is no other SDK there is nothing you can do.

    A very complex workaround might be to upload the file to your own backend. Then open the browser to gain access to an oauth token to upload the image via your backend. However this is a quiet expensive solution which I would avoid.