Search code examples
androidfacebookandroid-contentproviderandroid-sharing

Sharing image from app private storage to Facebook


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.


Solution

  • 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.