【问题标题】:Sharing an image with Google+ app using Intent.ACTION_SEND and Intent.EXTRA_STREAM使用 Intent.ACTION_SEND 和 Intent.EXTRA_STREAM 与 Google+ 应用共享图片
【发布时间】:2012-01-26 18:14:52
【问题描述】:

我的应用会生成用户可以保存或与他人共享的图像。下面的代码适用于大多数应用程序:Messenger、Facebook、Dropbox、电子邮件等。意思是,图像由所选应用程序加载,用户可以与该应用程序成功共享图像。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
File o = new File(dir, "file.png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(o));
startActivity(Intent.createChooser(intent , "Send options")); 

但是,当我在应用程序列表中选择 Google+ 时,google+ 会启动,但图片不包含在帖子窗口中。相反,google+ 会显示一条 Toast 消息,其中包含:

"You can only post photos stored on your device."

这有点令人困惑,因为图像位于外部 SD 卡上,即 /mnt/sdcard/AppDir/file.png。我正在使用最新更新的 Google+ 应用 (2.3.1.242969)。

还有其他与 google+ 共享图片的技巧吗?

谢谢。

更新:

我的应用会生成共享的图像,因此下面来自@chirag-shah 的示例并不直接适用。但是,使用 MediaStore 看起来是正确的想法。我已经确定了下面的基本代码:

void shareImage(int position) {
    File f = getFileFor(position);
    ContentValues values = new ContentValues(2);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/png");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent , "Send options")); 
}

这适用于 Google+ 和我测试过的所有其他应用。如果这不是最佳实践,我将保留这个问题。任何人都可以确认这是正确的方法吗?

【问题讨论】:

    标签: android android-intent google-plus


    【解决方案1】:

    很棒的收获!您可以指定 MediaStore Uri(类似于 content://media/external/images/media/42)而不是文件系统上的绝对路径。

    例子:

    public class MyActivity extends Activity {
      ...
      static final int IMAGE_REQUEST = 0;
    
      protected void pickImage() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, IMAGE_REQUEST);
      }
    
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_REQUEST) {
          Uri uri = data.getData();
    
          Intent intent = new Intent(Intent.ACTION_SEND);
          intent.setType("image/png");
          // uri looks like content://media/external/images/media/42
          intent.putExtra(Intent.EXTRA_STREAM, uri);
          startActivity(Intent.createChooser(intent , "Share"));
        }
      }
    }
    

    【讨论】:

    • 感谢您的提示。使用媒体商店看起来是正确的方法,您的帖子让我有了这个想法。
    【解决方案2】:

    感谢您提出这个问题!

    是的,Google+ 似乎只接受来自内容提供商的媒体 (content:// uris),而不是 file:// uris。所以我们需要先把图片放入MediaStore。更简单的方法是:

    MediaStore.Images.Media.insertImage(context.getContentResolver(), tmpFile.getAbsolutePath(), tmpFile.getName(), null);
    

    【讨论】:

      【解决方案3】:

      您收到此错误的原因是因为“在 sd 卡上”不是“在您的设备上”。

      关于这个问题的讨论可以在这里找到:https://groups.google.com/a/googleproductforums.com/forum/#!topic/google-plus-discuss/pdlOTM8JEXg/discussion

      在 Google 更新 Google+ 应用程序之前,通过 MediaStore 的解决方法似乎是唯一的解决方案。

      更新:上述解决方案仅适用于您想一次性共享图像的情况。如果您多次调用 ContentResolver.insert(...),您最终会在图库中多次显示该图像。我想出了以下解决方案,它试图获取图像的现有 id,如果它不存在,你可以插入它...

      Cursor c = getActivity().getContentResolver()
                  .query(Images.Media.EXTERNAL_CONTENT_URI,
                          null,
                          Images.Media.DATA + "=?",
                          new String[] { filePath }, null);
          if (c.getCount() > 0 && c.moveToFirst()) {
              Uri shareUri = Uri.withAppendedPath(
                      Images.Media.EXTERNAL_CONTENT_URI,
                      ""
                              + c.getInt(c
                                      .getColumnIndex(Images.Media._ID)));
              c.close();
      
              startActivity(Intent.createChooser(
                      new Intent(Intent.ACTION_SEND)
                              .putExtra(Intent.EXTRA_STREAM,
                                      shareUri).setType(
                                         "image/*"), ""));
          } else {
          // ContentResolver.insert(...) and use that uri...
          }
      

      【讨论】:

      猜你喜欢
      • 2014-03-09
      • 1970-01-01
      • 2020-02-13
      • 2023-03-10
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多