在 Android 上通过 ContentProvider 共享应用程序数据目录中的图像
我试图通过 ContentProvider 公开位于应用程序的 /data 目录中的 .png 文件,但没有调用 openFile
方法 query
。现在我只需要公开一个图像以共享给其他应用程序,如何将我的 Intent 设置为转到 openFile
而不是 query
?
Intent shareImageIntent = new Intent(Intent.ACTION_SEND);
shareImageIntent.setType("image/*");
shareImageIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(shareImageIntent, "Share image"));
Uri 看起来像哪里
content://my.package.contentprovider/fileName
还是我需要为此创建一个数据库并返回游标?
更新
因此,这似乎适用于除短信应用程序(这是我决定首先测试的)之外的所有内容,但我想支持共享到它。
这是相关的堆栈跟踪:
由以下原因引起:java.lang.IllegalArgumentException:查询 content://mypackage.myprovider/someImage.png 返回 null 结果。在 com.android.mms.ui.UriImage.initFromContentUri(UriImage.java:104) 在 com.android.mms.ui.UriImage.(UriImage.java:63) 在 com.android.mms.model.ImageModel.initModelFromUri(ImageModel.java:83) 在 com.android.mms.model.ImageModel.(ImageModel.java:65) 在 com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:481) 在 com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:375) ...
所以 SMS 应用程序正在执行查询,而不是直接从 openFile
读取,我手机上的每个其他应用程序似乎都会这样做(包括其他 Google 应用程序)
有谁知道我需要在这里返回什么正确地完成查询?我现在要去 AOSP 挖。
I'm trying to expose a .png file located in my application's /data directory through a ContentProvider but instead of reaching the openFile
method query
is being called. Now I only ever have a single image which I need to expose for sharing to other applications, how can I setup my Intent to goto openFile
instead of query
?
Intent shareImageIntent = new Intent(Intent.ACTION_SEND);
shareImageIntent.setType("image/*");
shareImageIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(shareImageIntent, "Share image"));
Where the Uri looks like
content://my.package.contentprovider/fileName
Or alternatively do I need to create a database for this and return a cursor?
UPDATE
So this appears to be working on everything except the SMS app (which is what I decided to test first) I would like to support sharing to it however.
Here's the relevant stack trace:
Caused by: java.lang.IllegalArgumentException: Query on
content://mypackage.myprovider/someImage.png returns null result. at
com.android.mms.ui.UriImage.initFromContentUri(UriImage.java:104) at
com.android.mms.ui.UriImage.(UriImage.java:63) at
com.android.mms.model.ImageModel.initModelFromUri(ImageModel.java:83)
at com.android.mms.model.ImageModel.(ImageModel.java:65) at
com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:481)
at
com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:375)
...
So the SMS app is performing a query instead of reading directly from openFile
, which every other app on my phone seems to do (including other Google apps)
Does anyone know what I need to return here to fullfil the query appropriately? I'm going to go AOSP digging now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在深入研究短信(实际上是彩信)应用程序的源代码后,这就是我想到的。
在
UriImage.initFromContentUri
内部,应用程序生成查询代码并假设Cursor
中有 2 个返回列,因此为了让您的
ContentProvider
能够与MMS 应用程序,您需要在query
中返回一个只有一行和两列的Cursor
(Images.Media.DATA 和 Images.Media.MIME_TYPE),其中合适的 数据。然后,MMS 将调用openFile
以实际检索图像。After digging through the source code of the SMS (MMS really) app this is what I came up with.
Inside
UriImage.initFromContentUri
the application makes the query code and assumes there are 2 returned columns in theCursor
So inorder for your
ContentProvider
to work with the MMS app, you need to return aCursor
inquery
that only has one row and the two columns (Images.Media.DATA & Images.Media.MIME_TYPE) with the appropriate data. The MMS will then make the call toopenFile
to actually retrieve the image.共享图像资源的一种更简单的方法是将其保存到外部存储(SD 卡),然后执行以下操作:
更新:
尝试使用
Update2
尝试将文件保存到媒体存储并 发送它:
然后使用
ContentProvider
和Cursor
最终更新:您的
ContentProvider
必须实现query(.. )
方法,它必须返回一个光标
。请参阅源代码UrlImage.initFromContentUri(..)
的代码(彩信应用内部使用)来查看光标是如何调用的。看看MatrixCursor
是否有符合要求。An easier way to share a image resource is to save it to external storage (SD-card) and then do:
Update:
Try using
Update2
Try saving file to Media Store and then sending it:
Final Update using
ContentProvider
andCursor
:Your
ContentProvider
must implementquery(..)
method and it must return aCursor
. See the source code ofUrlImage.initFromContentUri(..)
(which is internally used by MMS app) to see how cursor is called. Take a look at theMatrixCursor
if it fits the bill.如果您的内容提供程序已经正常工作,您可以通过方法 openFileDescriptor 在内容提供者中。
一个快速而肮脏的例子:
干杯!
If your content provider is already working you can access to a ParcelFileDescriptor via the method openFileDescriptor in the content provider.
A quick, and dirty, example for this:
Cheers!