【问题标题】:How to share http image directly to twitter in android?如何在android中直接将http图像分享到twitter?
【发布时间】:2016-02-17 12:07:18
【问题描述】:

到目前为止,我在 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 url 中显示。

任何人都知道如何在不显示链接的情况下直接将图像分享到 Twitter 应用程序。

【问题讨论】:

  • 为什么不使用 Twitter API?
  • 唯一具有网络意图的嵌入图像是具有pic.twitter.com 域的图像。
  • @oldergod 你能详细说明一下吗。我不明白你。

标签: android twitter android-twitter


【解决方案1】:

您可以使用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;
}

【讨论】:

    【解决方案2】:

    你可以试试它对我的工作

    图像 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.so 加载 http 图像,因此无法使用此代码
    • 我正在使用 Glide 在本地 /path/to/image 中加载 http 图像,然后再使用它
    • 如果不从文件路径获取图像,我必须直接将图像共享到 twitter。有可能吗?
    • 看看下面的帖子也许你需要的东西:)
    • 感谢您的回答。我会检查那个,稍后告诉你
    【解决方案3】:

    这就是你需要的

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

    【讨论】:

      【解决方案4】:

      打电话:

      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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 2017-11-01
        相关资源
        最近更新 更多