Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing images and text with Facebook on Android

Facebook, why you no take images and text in share Intent?

enter image description here

I'm trying to use the standard Android share Intent to share an image, and some text. My share Intent is set up right, I've got the image in there, and I've done my end of the deal. My code, let me show you it:

public void doShare() {
    File image = getShareFile();
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("*/*");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, resultObject.getShareSubject());
    shareIntent.putExtra(Intent.EXTRA_TEXT, resultObject.getShareText());
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
    shareActionProvider.setShareIntent(shareIntent);
}

Facebook shows up in the list of apps that will handle my Intent, but it lies!!! When clicking on Facebook to actual do a share it says:

Please attach only photos or a single video.

Why Facebook, why? Why do you show up as an app that can handle my Intent, then make me look stupid to my users by not handling the Intent? You promised Facebook.

I've seen many many threads on this all over this site, and the web. Has anyone gotten this to work without having to use their API?

like image 669
Christopher Perry Avatar asked Jan 09 '13 19:01

Christopher Perry


2 Answers

Why? Because they are Facebook and can permit to themselves such bugs. So, they expect only images upon type is "image/". If the type is "text/plain"-then a URL should be contained in the message. Don't use .setType("/*"); Otherwise use their SDK. But in this case you'll sacrifice simplicity and flexibility of your application.

like image 63
Simon Avatar answered Oct 09 '22 11:10

Simon


Solution to the following highlighted problems.

Please attach only photos or a single video.

Facebook isn't in the Share option Dialog

even if Facebook wall appears, then it won't let you open your image on the front page.

Share an image on Facebook without using Facebook SDK

I got lucky after searching for three days constantly. Sorry, The answer might be lengthy, because I found no solution on StackOverflow that could share an image on Facebook without using Facebook SDK.

Here is the full and perfect solution:

Bitmap bitmap = viewToBitmap(load your image OR layout background for your imageView);
shareImage(bitmap, "com.facebook.katana");

The first parameter is the imageView/layout for the images that are loaded into the bitmap. The second parameter is the facebook's package name.

Then code the following Method as shown.

public void shareImage(Bitmap bitmap, String packageName) {
        try {
            File file = new File(this.getExternalCacheDir(), "temp.png");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 95, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            file.setReadable(true, false);
            final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/*");
            intent.setPackage(packageName);
            startActivity(Intent.createChooser(intent, "Share image on facebook"));
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            Toast.makeText(this, "Error while sharing Image on Facebook", Toast.LENGTH_SHORT).show();
        }
    }

Here're my app Screenshots:

1. "Quote book App" is loading a picture with a quote on it.

2. Click the share button and click on Facebook from the dialog.

3. Facebook sharing options dialog appears.

4. Image loaded to the Facebook front page.

5. Finally the Image is shared on Facebook And the method is working properly as we expected.

PS: You can check out this functionality in my app, Here's the Link to download it from google play store.

like image 20
meyasir Avatar answered Oct 09 '22 11:10

meyasir