Gary Kipnis
Gary Kipnis

Reputation: 732

Facebook Upload of photos fails from Android Chooser

In my app, I have an option to share photos using standard Android sharing mechanism. When I choose gmail or yahoo mail, the sharing works, the images are displayed in the body of an email and are sent to the destination. When I choose Facebook, the images are displayed inside the Facebook activity where you can add a message, but when I hit the Done button, the upload process starts, but always fails.

The title of the message box is: "Facebook Upload Failed" and the message itself is: "Your upload could not be completed because we could not access your photo(s)."

Here is the code snippet for sharing:

mShareIntent = new Intent();
mShareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
mShareIntent.setType("image/jpeg");
mShareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
for(int i = 0; i < mImageUrls.size(); i++) {                    
    uris.add(Uri.parse("file://"+mShareFiles[i].getAbsolutePath()));
}
mShareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(mShareIntent,  "Share Images To..."))

The photos are obviously accessible, since they are successfully processed by gmail and yahoo mail. And Facebook, itself, can access them, but not sent them.

As an experiment, I shared photos from the Android Gallery, in the Chooser, I chose Facebook, and the photos were successfully uploaded.

Does anyone have any idea what might be going on?

Thank You, Gary

Upvotes: 0

Views: 851

Answers (1)

matiash
matiash

Reputation: 55370

If the file is in your app's private directory (or even in its sd card directory in KitKat) then you might run into sharing issues. And FileProvider has its problems too.

The only reliable way I've found to share image files is to copy them to the external storage directory for pictures and sharing from there (you can create a .nomedia subdirectory to avoid them appearing in the Gallery app).

For example:

Bitmap bitmapToShare = ...

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
    File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File noMedia = new File(pictureStorage, ".nomedia");
    if (!noMedia.exists())
        noMedia.mkdirs();

    File file = new File(noMedia, "shared_image.png");
    saveBitmapAsFile(bitmapToShare, file);

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    shareIntent.setType("image/png");

    startActivity(shareIntent);
}

Upvotes: 1

Related Questions