使用Intent.ACTION_SEND和Intent.EXTRA_STREAM与Google+应用分享图片

ste*_*n.z 19 android android-intent google-plus

我的应用会生成用户可以保存或与他人共享的图像.以下代码适用于大多数应用程序: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")); 
Run Code Online (Sandbox Code Playgroud)

但是,当我在应用列表中选择Google+时,Google +会启动,但图片不会包含在帖子窗口中.相反,谷歌+显示一个Toast消息:

"You can only post photos stored on your device."
Run Code Online (Sandbox Code Playgroud)

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

还有另一个与谷歌+共享图像的技巧吗?

谢谢.

更新:

我的应用会生成共享的图像,因此@ 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")); 
}
Run Code Online (Sandbox Code Playgroud)

这适用于Google+和我测试过的所有其他应用.我打开这个问题以防这不是最佳做法.任何人都可以确认这是正确的方法吗?

Chi*_*hah 9

很棒!您可以指定MediaStore Uri(看起来像内容:// 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"));
    }
  }
}
Run Code Online (Sandbox Code Playgroud)