谷歌+分享意图不能访问图像 [英] Share Intent of Google+ can not access image
问题描述
我打电话的意图,共享图像。这与大多数供应商,但与Google+的。 Google+的开启活动后没有图像,并显示土司你只能发布存储在您的设备上的照片。同时
i am calling an intent to share an image. this works with most providers, BUT with Google+. Google+ opens the post activity without the image and displays the toast "You can only post photos stored on your device." at the same time.
File f = storeImage(image); // f = /data/data/com.myapp/files/1333070776978.jpg
Uri uri = Uri.fromFile(f);
Intent share = new Intent(Intent.ACTION_SEND);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TITLE,"Share that title");
share.putExtra(Intent.EXTRA_SUBJECT,"Share that subject");
share.putExtra(Intent.EXTRA_TEXT,"Check that out...");
share.putExtra("sms_body", "sms body");
startActivity(Intent.createChooser(share, "Share Image"));
我保存的图片
i save the image with
Context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
我的理解是,通过设置FLAG_GRANT_READ_URI_PERMISSION,我给Google+的具体访问该文件。
my understanding was that by setting FLAG_GRANT_READ_URI_PERMISSION, i give Google+ specific access to this file.
它工作时,我的图像存储到MediaStore,但我其实不想杂波的用户图片库。
it works when i store the image into the MediaStore, but i actually don't wanna clutter the users image gallery.
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
任何建议是pciated AP $ P $
any advice is appreciated
西蒙
推荐答案
的Google+不能访问文件://
尤里斯从另一个应用程序的私有文件夹。而 FLAG_GRANT_READ_URI_PERMISSION
并没有在这种情况下工作,因为它仅适用于URI的数据的意图的一部分,而不是额外的。
Google+ cannot access file://
Uris from the private folder of another application. And the FLAG_GRANT_READ_URI_PERMISSION
doesn't work in this case because it's only for Uris in the "data" part of an Intent, but not for extras.
一个常用的解决方法是将它(通过 MediaStore
)添加到库中,但它没有必要这么做。在这种情况下,正确的解决方案是使用一个<一href="http://developer.android.com/reference/android/support/v4/content/FileProvider.html"><$c$c>FileProvider$c$c>:
One common workaround is to add it to the gallery (via the MediaStore
) but it's not necessary to do so. The correct solution in this case is to use a FileProvider
:
FileProvider ContentProvider的是一个特殊的子类,它有利于
文件安全共享同一个应用程序创建一个关联
内容://
的URI的文件,而不是一个文件:///
乌里
FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a
content://
Uri for a file instead of afile:///
Uri.
在AndroidManifest.xml中下面的第一名
First place the following in your AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.testshare.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
然后在此文件资源\ XML \ filepaths.xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="shared_images" path="shared_images/" />
</paths>
然后,共享图像文件时,请确保它在上面(即 shared_images
在 getFilesDir()$ C $指定的路径C>)和建立的意图如下:
Then, to share an image file, make sure it's in the path specified above (i.e. shared_images
under getFilesDir()
) and build the intent as follows:
File file = getImageFileToShare();
Uri fileUri = FileProvider.getUriForFile(this, "com.myapp.testshare.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/png");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
(请务必在 getUriForFile()
方法中指定的权威之一清单匹配)。
(Make sure the authority specified in the getUriForFile()
method matches the one in the manifest).
这将产生一个内容://
的URI(如内容://com.myapp.testshare.fileprovider/shared_images/img1.png
的Google+应用将能够访问,并因此包括在后)。
This will produce a content://
Uri (like content://com.myapp.testshare.fileprovider/shared_images/img1.png
that the Google+ app will be able to access, and thus include in the post).
这篇关于谷歌+分享意图不能访问图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!