Sharing image from app private storage to Facebook

☆樱花仙子☆ 提交于 2019-12-25 07:17:02

问题


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):

  1. I launch an intent with EXTRA_STREAM containing a Uri to an image inside the private directory of my app.
  2. Through a ContentProvider, I provide access to the desired file by returning the following in openFile():

    return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    
  3. 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.


回答1:


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.



来源:https://stackoverflow.com/questions/38547465/sharing-image-from-app-private-storage-to-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!