共享的图像,使用Intent.ACTION_SEND和Intent.EXTRA_STREAM Google+应用 [英] Sharing an image with Google+ app using Intent.ACTION_SEND and Intent.EXTRA_STREAM

查看:144
本文介绍了共享的图像,使用Intent.ACTION_SEND和Intent.EXTRA_STREAM Google+应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序生成的图像,用户可以保存或与其他人分享。下面的code适用于大多数应用程序:信使,脸谱,Dropbox的,电子邮件等含义,图像是由所选择的应用程序加载,用户可以使用该应用程序成功地将影像分享的

My app generates images that a user can save or share with others. The code below works for most apps: Messenger, Facebook, Dropbox, email, etc. Meaning, the image is loaded by the chosen app, and a user can share the image successfully with that app.

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+,谷歌+启动,但图像没有包含在后窗口。相反,谷歌+与显示一条吐司消息:

However, when I choose Google+ in the list of apps, google+ starts, but the image is not included in the post window. Instead, google+ displays a Toast message with:

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

这是一个有点混乱,因为图像是外置SD卡,即/mnt/sdcard/AppDir/file.png上。我现在用的Google+应用最新的更新(2.3.1.242969)。

This is a little confusing, because the image is on the external SD card, i.e. /mnt/sdcard/AppDir/file.png. I am using the latest update of Google+ app (2.3.1.242969).

有另一种伎俩与谷歌分享图片+?

Is there another trick to sharing an image with google+?

感谢你。

更新:

我的应用程序生成共享图像,所以下面的样本来自@ CHIRAG - 沙阿是不能直接适用。但是,使用MediaStore貌似正确的想法。我已经谈妥的基本$ C $低于C:

My app generates the images that are shared, so the sample below from @chirag-shah wasn't directly applicable. But, using the MediaStore looks like the right idea. I've settled on the basic code below:

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+的,我已经与测试的所有其他应用程序。我要离开的问题的情况下,开放式,这不是最好的做法。任何人都可以证实,这是正确的方式做到这一点?

This works with Google+ and all other apps I've tested with. I'm leaving the question open in case this is not best practice. Can anyone confirm that this is the right way to do it?

推荐答案

钻石王老五!您可以指定MediaStore URI(它看起来像内容://媒体/外部/图片/媒体/ 42 ),而不是在文件系统上的绝对路径

Great catch! You can specify the MediaStore Uri (which looks like content://media/external/images/media/42) instead of the absolute path on the file system.

例如:

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

这篇关于共享的图像,使用Intent.ACTION_SEND和Intent.EXTRA_STREAM Google+应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆