【问题标题】:how to share a single photo in android?如何在android中分享单张照片?
【发布时间】:2013-11-21 10:21:25
【问题描述】:

我将一张照片分享到 facebook、twitter、Linkedin、picasa。我可以毫无问题地分享文字。任何人都可以用一些例子来解释如何分享一张照片。目前我正在使用以下代码(添加一个简单的分享操作)

private ShareActionProvider mShareActionProvider;

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    //Uri screenshotUri = Uri.fromFile(new File(getFilesDir(), ".jpg"));
    Log.d("Storage dir ", "Getting the directory");
    File f = FileUtils.getStorageDir();
    Log.d("All Answers: ", f.getAbsolutePath());
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM,f);      
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));
    // Set the share Intent
    mShareActionProvider.setShareIntent(sharingIntent);
    return true;
  }

  // Call to update the share intent
  private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
  }
}

提前致谢

【问题讨论】:

标签: android android-sharing


【解决方案1】:

共享二进制对象(图像、视频等) 您可以使用此代码

除了支持文本外,此意图还支持共享图像或任何二进制内容。您所要做的就是设置适当的 mime 类型,然后通过调用 put Extra 方法传递二进制数据。

 Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);

sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

注册 Intent

如果您希望在调用此 Intent 时列出您的应用,则必须在 manifest.xml 文件中添加一个 Intent 过滤器

<intent-filter>
   <action android:name="android.intent.action.SEND" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="image/*" />
</intent-filter>

您将打开选择器对话框并在 facebook、twitter、Linkedin 等上选择 Facebook 和共享照片。

【讨论】:

    【解决方案2】:

    SocialLib 是一个优秀的安卓分享工具包(iOS 最好是ShareKit)。

    SocialLib 允许集成: Facebook 推特 谷歌嗡嗡声 领英

    如果您只想使用 facebook 分享,请关注 Facebook Share Dialog

    【讨论】:

      【解决方案3】:

      试试这个代码:

          Intent share = new Intent(Intent.ACTION_SEND);
          share.setType("image/*");
          share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
      

      它将打开选择器对话框,其中包含允许照片共享功能的所有可能的应用程序。只需选择 Facebook 、Twitter 或您要在其上共享照片的任何其他应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-17
        • 1970-01-01
        • 2020-05-28
        • 2014-07-05
        • 1970-01-01
        相关资源
        最近更新 更多