2015-10-19 255 views
0

我正在使用下面的代码在twitter上分享视频。它工作正常,如果应用程序安装在设备上,但不工作,如果应用程序不存在。我可以在Twitter上分享视频,即使如果应用程序未安装在设备中?Twitter分享,如果应用程序没有安装

File f = new File(extras.getString("filepath")); 
       Intent shareIntent = new Intent(Intent.ACTION_SEND); 
       shareIntent.setType("video/*"); 
       shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); 
       final PackageManager pm = mActivity.getApplicationContext().getPackageManager(); 
       final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0); 

       for (final ResolveInfo app : activityList) { 

        if (("com.twitter.android.composer.ComposerActivity".equals(app.activityInfo.name)) || ("com.twitter.android.PostActivity".equals(app.activityInfo.name))) { 

         final ActivityInfo activity = app.activityInfo; 
         final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name); 
         shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
         shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
         shareIntent.setComponent(name); 
         mActivity.getApplicationContext().startActivity(shareIntent); 

         break; 
        } 
       } 

回答

1

你可以使用Twitter的Fabric SDK

的实现将是这样的:

在你settings.gradle:

buildscript { 
repositories { 
    jcenter() 
     maven { url 'https://maven.fabric.io/public' } 
} 
dependencies { 

    classpath 'io.fabric.tools:gradle:1.+' 
} 
} 

allprojects { 
repositories { 
    jcenter() 
    maven { url 'https://maven.fabric.io/public' } 
} 
} 

在你的build.gradle中添加以下的依赖:

compile('com.crashlytics.sdk.android:crashlytics:[email protected]') { 
    transitive = true; 
} 
compile('com.twitter.sdk.android:tweet-composer:[email protected]') { 

    transitive = true; 
} 
compile('com.twitter.sdk.android:twitter:[email protected]') { 
    transitive = true; 
} 
compile 'org.apache.directory.studio:org.apache.commons.io:2.4' 

,然后在AndroidManifest.xml中添加以下内容:

<application> 
... 
<meta-data 
     android:name="io.fabric.ApiKey" 
     android:value="YOUR FABIRC API KEY" /> 
</application> 

最后到你的活动使用它:

导入以下:

import com.twitter.sdk.android.core.TwitterAuthConfig; 
import com.twitter.sdk.android.core.TwitterCore; 
import com.twitter.sdk.android.tweetcomposer.TweetComposer; 
import io.fabric.sdk.android.Fabric; 

在你的OnCreate功能做:

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Fabric.with(this, new Crashlytics()); 

TwitterAuthConfig authConfig = new TwitterAuthConfig("consumerKey", "consumerSecret"); 
Fabric.with(this, new TwitterCore(authConfig), new TweetComposer()); 

然后派:

TweetComposer.Builder builder = new TweetComposer.Builder(this).text(sendText); 
File myFile = new File("path to your file"); 
Uri myUri = Uri.fromFile(myFile); 
builder.image(myUri); 
builder.show(); 
+0

与twitter无关的方式都完全不同 –

+0

@Nic我可以通过Tweet Composer分享视频吗?我可以发布一些代码吗? –

+0

我可以分享视频wid TweetComposer吗? –

0

为了实现这一目标,你必须使用Twitter的API和后期用户的帐户,或者使用其提供的SDK这里的链接到Android SDK https://dev.twitter.com/overview/api/twitter-libraries 也使用Twitter 4J,帮助你轻松的事情,使用API​​更容易编码。 http://twitter4j.org/en/

+0

你可以张贴一些代码或链接? –

+0

检查更新后的答案 –

+0

@bhuvi是链接有用 –

相关问题