从应用专用存储共享图像到Facebook [英] Sharing image from app private storage to Facebook
问题描述
- 我将包含Uri的EXTRA_STREAM的意图发送到我应用程序私有目录中的图像。
-
通过ContentProvider,我可以通过在
openFile()
中返回以下内容来提供对所需文件的访问:return ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
-
分享到Facebook时,图像不会显示。然而,每个其他应用程序都可以正常工作将调用调用到我的ContentProvider,我看到Facebook确实查询了ContentProvider,并且
openFile()
被命中。不过,图像不会出现。
,我意识到我正在返回 null
作为MIME类型。我改变了 getType()
的结果返回image / png,就是这样:Facebook接受了我的形象:
@Nullable
@Override
public String getType(@NonNull Uri uri){
//绝对必须提供MIME类型否则一些应用程序,如
// Facebook将简单地忽略该文件
returnimage / png;
}
我会指出你应该返回实际的MIME类型与Uri相关联的文件;我在这里返回PNG,因为我很懒,我知道我的所有图像都是这种类型。
I'm attempting to take a very generic approach in providing sharing options for sharing images from my app's private storage, but I've encountered a problem that appears to be specific to sharing images to the Facebook app (com.facebook.katana):
- I launch an intent with EXTRA_STREAM containing a Uri to an image inside the private directory of my app.
Through a ContentProvider, I provide access to the desired file by returning the following in
openFile()
:return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
When sharing to Facebook, the image doesn't appear. Every other app, however, does work. Debugging calls into my ContentProvider, I see that Facebook does indeed query the ContentProvider, and
openFile()
is hit. Nevertheless, the image doesn't appear.
After much head scratching, I realized I was returning null
as the MIME type. I changed the result of getType()
to return "image/png", and that was it: Facebook accepted my image:
@Nullable
@Override
public String getType(@NonNull Uri uri) {
// It's absolutely imperative that we provide the MIME type, otherwise some apps like
// Facebook will simply ignore the file
return "image/png";
}
I would point out that you should return the actual MIME type of the file associated with the Uri; I'm returning PNG here because I'm lazy, and I know that all my images are of that type.
这篇关于从应用专用存储共享图像到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!