小编ste*_*n.z的帖子

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

我的应用会生成用户可以保存或与他人共享的图像.以下代码适用于大多数应用程序: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+和我测试过的所有其他应用.我打开这个问题以防这不是最佳做法.任何人都可以确认这是正确的方法吗?

android android-intent google-plus

19
推荐指数
1
解决办法
8431
查看次数

标签 统计

android ×1

android-intent ×1

google-plus ×1