Android 与谷歌共享图像+;使用Intent.ACTION\u发送和Intent.EXTRA\u流的应用程序
我的应用程序生成用户可以保存或与他人共享的图像。下面的代码适用于大多数应用程序:Messenger、Facebook、Dropbox、电子邮件等。这意味着图像由所选应用程序加载,用户可以与该应用程序成功共享图像Android 与谷歌共享图像+;使用Intent.ACTION\u发送和Intent.EXTRA\u流的应用程序,android,android-intent,google-plus,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_ST
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+会启动,但图像不会包含在post窗口中。相反,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+和我测试过的所有其他应用程序。如果这不是最佳实践,我将保留问题。有人能证实这是正确的方法吗?太棒了!您可以指定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"));
}
}
}
出现此错误的原因是“在sd卡上”不是“在设备上” 有关此问题的讨论可在此处找到: 在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...
}
谢谢你提出这个问题 是的,Google+似乎只接受来自内容提供商(content://uris)的媒体,而不接受file://uris。因此,我们需要先将图像放入MediaStore。更简单的方法是:
MediaStore.Images.Media.insertImage(context.getContentResolver(), tmpFile.getAbsolutePath(), tmpFile.getName(), null);
谢谢你的提示。使用媒体商店看起来是正确的选择,你的帖子给了我这个想法。似乎这并不总是奏效: