ban*_*ing 7 android share image android-contentprovider google-plus
我已经使用了这段代码,并且可以通过我的Android应用程序成功地将图像(来自我手机的图库)与文本分享到谷歌+.
但是,当我尝试从我的应用内容提供商发布图片时,图片会显示在我的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);
Run Code Online (Sandbox Code Playgroud)
...而且,就像我说的,如果localImageUri值是针对我手机图库中的资源,则图像会成功显示在最终的Google +页面上 - 如果我localImageUri从应用程序自己的内容提供商设置为uri,则显示上面的占位符图像.
所以我认为我的内容提供商一定存在问题,我的清单中将其定义为:
<provider
android:name=".DbContentProvider"
android:authorities="com.example.provider"
android:exported="true"
android:grantUriPermissions="true" />
Run Code Online (Sandbox Code Playgroud)
那么,我的清单中是否会遗漏一些东西 - 甚至是我的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>
Run Code Online (Sandbox Code Playgroud)
如果没有,那么问题是什么呢?
更新(遵循CommonsWare的评论)
异常确实是由Google+信息库抛出,因为也做在查询_data栏(其存在),借助Google+ API,也一直在寻找一个不存在的列-即datetaken,date_added,date_modified.
所以我已经将这些列添加到我的数据库表中 - 将它们全部添加为text具有最近millis值的列,例如'1419379390000'(ref here)但我仍然在我的Google+页面上显示相同的占位符图像.
所以我query()在我的DbContentProvider类的方法中添加了一些日志记录代码,当google +执行单列查询时datetaken,游标中返回的值确实是1419379390000.但是,(单独)_data查询返回的值是null.
我不确定为什么谷歌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);
Run Code Online (Sandbox Code Playgroud)
...但是我的光标中的_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)
Run Code Online (Sandbox Code Playgroud)
...它会调出g +屏幕来自定义共享消息(我确认,它确实包含来自我的内容提供商的图像).然后我点击g +屏幕上的Share按钮,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"
Run Code Online (Sandbox Code Playgroud)
2015年1月23日更新
这是我的全部清单:
<?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>
Run Code Online (Sandbox Code Playgroud)
这是一个有趣的问题。如果您的ContentProvider导出(问题中并不完全清楚 - 它似乎在您发布的第一个片段中,但不在稍后添加的完整 AndroidManifest.xml 中),您所做的应该有效。不知道为什么没有。
无论如何,您当然可以通过以下方式将任何图像文件共享到 Google+ 应用程序FileProvider:
content://FileProvider 是 ContentProvider 的一个特殊子类,它通过为文件而不是Uri创建 Uri 来促进与应用程序关联的文件的安全共享file:///。
因此,如果您的图像实际上是(很可能)下面的文件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>
Run Code Online (Sandbox Code Playgroud)
然后这个文件在res\xml\filepaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="shared_images" path="shared_images/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
然后,要共享图像文件,首先确保它位于上面指定的路径中(即,如果shared_images存在,getFilesDir()则无需WRITE_EXTERNAL_STORAGE复制权限,因为该位置位于您自己的应用程序的私有存储区域内)。然后构建共享意图,如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
(确保getUriForFile()方法中指定的权限与清单中的权限匹配)。
这将生成一个content://Uri(就像content://com.example.testshare.fileprovider/shared_images/img1.pngGoogle+ 应用程序能够访问并包含在帖子中一样)。
| 归档时间: |
|
| 查看次数: |
688 次 |
| 最近记录: |