2012-06-08 51 views
1

我試圖添加共享意圖發佈到谷歌加似乎並不能化解傳遞新ShareCompat.IntentBuilder(支持Android庫類)的問題向startActivity方法。我開始使用this example。我的應用程序是使用Android 2.2平臺編譯的。是否有另一種支持方式來啓動活動來啓動共享意圖。如何用ShareCompat.IntentBuilder startActivity

IntentBuilder shareIntent = ShareCompat.IntentBuilder.from(MyActivity.this);     
shareIntent.setText(message); 
shareIntent.setSubject(subject); 

if (mFullFileName != null) { 
    File imageFile = new File(mFullFileName); 
    if (imageFile.exists()) { 
     shareIntent.setStream(Uri.fromFile(imageFile)); 
     shareIntent.setType("image/jpeg"); 
    } 
} else { 
    shareIntent.setType("*.*"); 
} 
shareIntent.getIntent(); 
// doesn't compile only accepts Intent and not the Intentbuilder 
startActivity(shareIntent); 
+0

笏是你所得到的錯誤?如果可能,放入logcat輸出。 – havexz

+0

對不起..我的帖子並沒有我第一次想到的那麼準確..我給startActivity方法添加了一個註釋塊..它的編譯問題似乎無法爲我工作。 – user167698

回答

4

這是我的代碼的一個例子,但如果你想要一些參考資料在超鏈接上點擊一篇文章。

public void shareText(String text) { 
     String mimeType = "text/plain"; 
     String title = "Example title"; 

     Intent shareIntent = ShareCompat.IntentBuilder.from(this) 
                .setChooserTitle(title) 
                .setType(mimeType) 
                .setText(text) 
                .getIntent(); 
     if (shareIntent.resolveActivity(getPackageManager()) != null){ 
      startActivity(shareIntent); 
     } 
    } 

Blog post on ShareCompat.IntentBuilder and sharing intents

6

滑稽的,我只是理解了它......給被假設創建一個Intent,而不是一個IntentBuilder對象的例子..有我的代碼更改爲鏈中的對象創建。

Intent i = ShareCompat.IntentBuilder.from(MyActivity.this) 
         .setText(message) 
         .setSubject(subject) 
         .setStream(Uri.fromFile(imageFile)) 
         .setType("image/jpeg") 
         .getIntent() 
         .setPackage("com.google.android.apps.plus"); 
+0

你可以接受你自己的答案......我刪除了我的,以減少噪音。 – havexz

+0

感謝您的努力,以幫助.. :-) – user167698

+0

請注意,Uri.fromFile(imageFile)是不安全的,因爲牛軋糖。 「在包域外傳遞file:// URIs可能會給接收器留下難以訪問的路徑。」 https://developer.android.com/about/versions/nougat/android-7.0-changes.html#perm –