【发布时间】: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