如何在安卓设备上直接分享http图片到Twitter?

6

到目前为止,我在stackoverflow上搜索了一些帖子,我可以直接分享文本到Twitter而不显示弹出对话框。这意味着当我点击按钮时,它会直接重定向到Twitter应用程序并显示文本。

我唯一的问题是我必须直接分享http图像到Twitter。

下面是我迄今为止尝试过的代码:

UsersAdapter.java:

// Create intent using ACTION_VIEW and a normal Twitter url:
        String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s",
                urlEncode(strShareText), 
                urlEncode(strShareImageUrl));
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl));

        // Narrow down to official Twitter app, if available:
        List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo info : matches) {
            if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter")) {
                intent.setPackage(info.activityInfo.packageName);
            }
        }

        context.startActivity(intent);

在上面的代码中,文本在 Twitter 中显示正确,但图像显示为 http 链接。有人知道如何直接将图像分享到 Twitter 应用程序而无需显示链接吗?

为什么不使用 Twitter API? - vjsantojaca
只有带有 pic.twitter.com 域名的嵌入式图像才能使用 Web Intent。 - oldergod
@oldergod,您能否详细说明一下?我没有理解您的意思。 - Stephen
@Naruto https://onlinejournalismblog.com/2015/02/11/how-to-make-a-tweetable-image-in-your-blog-post/ - oldergod
4个回答

5

您可以使用 TwitterComposer 对话框。

这是示例代码。

TweetComposer.Builder builder = new TweetComposer.Builder(mActivity)
                .text("hello text");
                .image(Uri.parse("https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2016/01/07-responsive-image-example-castle-7-opt.jpg"));
builder.show();

在 gradle 文件中添加依赖项。
compile('com.twitter.sdk.android:twitter:1.8.0@aar') {
        transitive = true;
}

1
你可以试一下,这对我有效。 图片 Uri:
File myImageFile = new File("/path/to/image");
Uri myImageUri = Uri.fromFile(myImageFile);

意图构建器
TweetComposer.Builder builder = new TweetComposer.Builder(this)
   .text("just setting up my Fabric.")
   .image(myImageUri);
builder.show();

将以下内容添加到您的build.gradle依赖项中:

dependencies {
 compile('com.twitter.sdk.android:tweet-composer:1.0.5@aar') {
    transitive = true;
 }
}

你是从文件路径显示图片,对吧?我需要从API加载HTTP图片,所以这段代码不能用。 - Stephen
我正在使用Glide在本地路径/path/to/image中加载http图像,以便在使用它之前。 - MrZ
不从文件路径获取图像,我是否可以直接分享图像到 Twitter?这可行吗? - Stephen
看一下下面的帖子,也许是你需要的 :) - MrZ
谢谢你的回答。我会检查一下并稍后告诉你。 - Stephen

0

这就是你需要的

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);

0

调用:

new PostPicture(MyActivity.this).execute("IMAGE_URL");

发布图片:

private class PostPicture extends AsyncTask<String, Void, File> {
    private final Context context;

    public PostPicture(Context context) {
        this.context = context;
    }

    @Override
    protected File doInBackground(String... params) {

        FutureTarget<File> future = Glide.with(getApplicationContext())
                .load(params[0])
                .downloadOnly(600, 600);

        File file = null;
        try {
            file = future.get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return file;
    }

    @Override
    protected void onPostExecute(File result) {
        if (result == null) {
            return ;
        }

        Uri uri = FileProvider.getUriForFile(getApplicationContext(), 
         getApplicationContext().getPackageName(), result);
        postIt(uri);
    }

    private void postIt(Uri result) {
        TweetComposer.Builder builder = new TweetComposer.Builder(this)
                .text("Post Image from URl.")
                .image(result);
        builder.show();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接