问题描述
我正在使用下面的代码在Twitter上共享视频。如果在设备中安装了app,则可以正常工作,但如果没有将app安装在设备中则无法正常工作。即使未在设备中安装app,如何在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的 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();
2楼
要实现这一点,您必须使用Twitters api并发布到用户帐户或使用他们提供的SDK,这是Android SDK的链接也使用Twitter 4j帮助您可以简化工作,并使用api更轻松地编写代码。