2015-09-14 44 views
1

我必須將我的應用程序中的圖像分享到instagram。我已經通過Instagram開發者文檔,他們只提及通過Intent方法共享,但我需要的是我必須在發佈成功後得到回調。分享到Instagram並獲得回撥

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

有沒有辦法做到這一點?

+0

@rekire ofcourse將得到一個回調startActivityForResult。但我們不能確定用戶是否共享。如果你按回退也startActivityForResult將被稱爲 – droidev

+1

他們的API是安靜的基本:https://instagram.com/developer/mobile-sharing/android-intents/沒有更多的信息提供。 – rekire

+0

那麼如果我沒有在手機上安裝Instagram,該怎麼辦? – droidev

回答

1

根據他們的API有幾個選項。起初是指他們的Android Intents,這是一個不好的例子恕我直言,這或多或少是Android中如何共享數據的默認方式。你想要什麼更像是打開他們的應用程序。對於這樣的情況是存在的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標記以通過後端上傳圖像。然而,這是一個安靜的昂貴的解決方案,我會避免。