草庐IT

Android/Google Plus - 无法分享来 self 的内容提供商的图片

coder 2023-11-26 原文

我用过this code并且可以从我的 Android 应用程序成功地将带有文本的图片(来 self 手机的图库)分享到 google+。

但是,当我尝试从我的应用程序的内容提供商发布图片时,该图片会像这样显示在我的 google+ 页面上...

...尽管预期的图像在 google+ 应用程序预览屏幕上显示正常。

我用来分享的代码是:

String message = "My message"; 
Uri localImageUri = ContentUris.withAppendedId(DbContentProvider.CONTENT_URI_PRODUCTS, mProductId;
PlusShare.Builder builder = new PlusShare.Builder(getActivity());
builder.setText(message);
builder.addStream(localImageUri);
builder.setType("image/jpeg");
Intent shareIntent = builder.getIntent();
startActivityForResult(shareIntent, RC_GOOGLE_PLUS);

...而且,就像我说的,如果 localImageUri 值是我手机图库中的资源,则图像会成功显示在最终的 google+ 页面上 - 而上面的占位符图像会显示,如果我将 localImageUri 设置为来 self 的应用程序自己的内容提供商的 uri。

所以我认为我的内容提供者一定有问题,这在我的 list 中定义为:

<provider
    android:name=".DbContentProvider"
    android:authorities="com.example.provider"
    android:exported="true"
    android:grantUriPermissions="true" />

那么,我的 list 或什至我的 searchable.xml 文件中是否缺少某些内容:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"

    android:searchSuggestAuthority="com.example.provider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="content://com.example.provider/suggest"
    android:searchSuggestThreshold="3"

    android:includeInGlobalSearch="true"
    android:searchSettingsDescription="@string/search_settings_description"
    android:queryAfterZeroResults="true"
    android:voiceSearchMode="showVoiceSearchButton" >   
</searchable>

如果不是,那可能是什么问题?

更新(根据 CommonsWare 的评论)

Google+ 库确实抛出了异常,因为除了对 _data 列(存在)进行查询外,Google+ API 还在寻找不存在的列 - 即 datetakendate_addeddate_modified

因此,我已将这些列添加到我的数据库表中 - 将它们全部添加为 text 列,其中包含最近的毫秒值,例如 '1419379390000'(引用 here )但是我的 Google+ 页面上仍然显示相同的占位符图片。

所以我在 DbContentProvider 类的 query() 方法中添加了一些日志记录代码,当 google+ 对 datetaken 进行单列查询时>,游标中返回的值确实是 1419379390000。但是,(单独的)_data 查询返回的值是 null

我不确定为什么 google API 查询 _data 列(因为当我从数据库中检索图像时不需要在我的代码中调用它以显示在UI - 相反,我调用...

Uri localImageUri = ContentUris.withAppendedId(DbContentProvider.CONTENT_URI_PRODUCTS, mProductId);
InputStream in = cr.openInputStream(localImageUri);
Bitmap img = BitmapFactory.decodeStream(in);
imageView.setImageBitmap(img);

...但我的游标中 _data 值的 null 可能是问题所在。不过,不确定从哪里开始解决这个问题??

2014 年 12 月 30 日更新

这是 LogCat 输出。所以,当我点击我的 g+ 按钮时......

12-30 21:45:34.344: D/DbContentProvider(24633): getType(content://com.example.provider/products/1668)
12-30 21:45:34.534: D/DbContentProvider(24633): getType(content://com.example.provider/products/1668)
12-30 21:45:34.544: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)
12-30 21:45:34.584: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)
12-30 21:45:34.604: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)
12-30 21:45:34.604: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)
12-30 21:45:34.624: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)

...这会打开用于自定义共享消息的 g+ 屏幕(我确认,其中确实包含来 self 的内容提供商的图像)。因此,我然后单击 g+ 屏幕上的“共享”按钮,LogCat 输出为...

12-30 21:45:57.526: D/DbContentProvider(24633): openFile(content://com.example.provider/products/1668, r)
12-30 21:45:57.576: D/DbContentProvider(24633): getType(content://com.example.provider/products/1668)
12-30 21:45:57.576: D/DbContentProvider(24633): DbContentProvider -  query(content://com.example.provider/products/1668)
12-30 21:45:57.576: D/DbContentProvider(24633):  -      projection: {"datetaken"}
12-30 21:45:57.576: D/DbContentProvider(24633):  -      selection: null
12-30 21:45:57.576: D/DbContentProvider(24633):  -      selectionArgs: null
12-30 21:45:57.576: D/DbContentProvider(24633):  -      sortOrder: null
12-30 21:45:57.576: D/DbContentProvider(24633): SQL (without selectionArgs): SELECT datetaken FROM products WHERE (_id = 1668) LIMIT 1
12-30 21:45:57.596: D/DbContentProvider(24633): returned value: 1419379390000
12-30 21:45:57.596: D/DbContentProvider(24633): cursor count: 1
12-30 21:46:03.642: D/DbContentProvider(24633): getType("content://com.example.provider/products/1668") - returns "vnd.android.cursor.item/vnd.example.elemental"

2015 年 1 月 23 日更新

这是我的完整 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="10"
    android:versionName="0.10" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="20" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.example.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.example" />
            </intent-filter>
        </receiver>

        <service android:name=".GcmIntentService" />

        <provider
            android:name=".DbContentProvider"
            android:authorities="com.example.provider"
            android:exported="false"
            android:grantUriPermissions="false" >
            <grant-uri-permission android:pathPrefix="/products" />
        </provider>

        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" >
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </activity>
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter android:label="@string/app_name_short">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name_short"
            android:windowSoftInputMode="adjustPan" >
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />
        </activity>
        <activity
            android:name=".SearchResultsActivity"
            android:label="@string/app_name_short"
            android:launchMode="singleTop"
            android:parentActivityName=".HomeActivity"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />
        </activity>
        <activity
            android:name=".SingleShoppingListActivity"
            android:label="@string/shopping_list"
            android:parentActivityName=".HomeActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name=".SingleProductActivity"
            android:label="@string/shopping_list_item"
            android:parentActivityName=".HomeActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name=".InfoMenuActivity"
            android:label="@string/why_gmo_free"
            android:parentActivityName=".HomeActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name=".InfoContentActivity"
            android:label="@string/why_gmo_free"
            android:parentActivityName=".InfoMenuActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".InfoMenuActivity" />
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/settings"
            android:parentActivityName=".HomeActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name=".TwitterCallbackActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="twitter"
                    android:scheme="oauth" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DummyActivity"
            android:label="@string/title_activity_dummy" >
        </activity>
        <activity
            android:name=".SmartSearchResultsActivity"
            android:label="@string/app_name_short"
            android:parentActivityName=".SearchResultsActivity"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".SearchResultsActivity" />
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />
        </activity>
        <activity
            android:name=".WebViewActivity"
            android:label="@string/app_name_short"
            android:parentActivityName=".HomeActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name=".UpgradeActivity"
            android:label="@string/upgrade_to_pro_qm" >
        </activity>
    </application>

</manifest>

最佳答案

这是一个有趣的问题。如果您的 ContentProvider 已导出(问题中并不完全清楚 - 它似乎在您发布的第一个 fragment 中,但不在稍后添加的完整 AndroidManifest.xml 中)您在做什么应该 工作。不知道为什么没有。

无论如何,您当然可以通过 FileProvider 将任何图像文件共享到 Google+ 应用程序:

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 a file:/// Uri.

因此,如果您的图像实际上是 getFilesDir() 下的一个文件(很有可能)或者至少可以移动到那里,那么您可以应用此解决方案。

首先,您必须在 AndroidManifest.xml 文件中定义提供程序:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.testshare.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

然后在 res\xml\filepaths.xml 中的这个文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="shared_images" path="shared_images/" />
</paths>

然后,要共享一个图像文件,首先确保它在上面指定的路径中(即 getFilesDir() 下的 shared_images -- 你不需要 WRITE_EXTERNAL_STORAGE 复制权限,因为这个位置在您自己的应用程序的私有(private)存储区域内)。然后按如下方式构建共享 Intent :

File file = getImageFileToShare();
Uri fileUri = FileProvider.getUriForFile(this, "com.example.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() 方法中指定的权限与 list 中的权限相匹配)。

这将生成一个 content:// URI(如 content://com.example.testshare.fileprovider/shared_images/img1.png,Google+ 应用将能够访问,从而包含在帖子中)。

关于Android/Google Plus - 无法分享来 self 的内容提供商的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591480/

有关Android/Google Plus - 无法分享来 self 的内容提供商的图片的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  9. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  10. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

随机推荐