【问题标题】:Twitter share if app not installed如果未安装应用程序,则 Twitter 共享
【发布时间】:2015-10-19 13:06:43
【问题描述】:

我正在使用以下代码在 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;
                    }
                }

【问题讨论】:

    标签: android android-intent twitter share


    【解决方案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:2.5.2@aar') {
        transitive = true;
    }
    compile('com.twitter.sdk.android:tweet-composer:0.9.0@aar') {
    
        transitive = true;
    }
    compile('com.twitter.sdk.android:twitter:1.8.0@aar') {
        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();
    

    【讨论】:

    • 与推特没有任何关系,两者完全不同
    • @Nic 我可以通过 Tweet Composer 分享视频吗?你能发布一些代码吗?
    • 我可以通过 TweetComposer 分享视频吗?
    • @Bhuvi 嘿,我已经更新了代码。我只用图像进行了测试,但我的想法是你可以用同样的方式传递视频的 uri。
    • +one 为你的答案,但我不认为推文作曲家适用于视频..我以前用它来处理图像..如果它也适用于你的视频,那么让我知道
    【解决方案2】:

    要实现这一点,您必须使用 Twitters api 并发布到用户的帐户或使用他们提供的 SDK,这里是 Android Sdk https://dev.twitter.com/overview/api/twitter-libraries 的链接 还可以使用 Twitter 4j 来帮助您简化事情并使用 api 更轻松地编写代码。 http://twitter4j.org/en/

    【讨论】:

    • @bhuvi 是有用的链接
    • 遗憾的是 Twitter4J 不支持推特视频
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 2018-12-23
    • 2019-06-18
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多