PerfectGamesOnline.com
PerfectGamesOnline.com

Reputation: 1778

Share image in Facebook programmatically from Android app via Intent

I have this code working well on Android 4.0.4.

// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);

// Set the MIME type
share.setType(type);

// Create the URI from the media
java.io.File media = new java.io.File(mediaPath);
Uri uri = Uri.fromFile(media);

// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);

// Broadcast the Intent.
mActivity.startActivity(Intent.createChooser(share, "Share to"));

But on Android 4.4.2 it crashes the Facebook app. Facebook app opens, the image is not shown and the FB app is dead.

In log dump I've noticed this message:

E/JHEAD   ( 5850): can't open '/data/data/cz.volten.brili.android.free/files/product_preview_shared.jpg'
V/ContextImpl( 5850): ----- packageName = com.facebook.katana is NOT LOCKED -----

Could the reason be some security restrictions, e.g. The FB app does not have rights to access the image in the application folder even though it is invoked from an intent?

If so, what would be a proper location for an image shared between the apps?

Shall I use something like this: how to share image to facebook via intent

Upvotes: 0

Views: 5728

Answers (3)

DEVSHK
DEVSHK

Reputation: 863

This code works for me.....here "updateImage" is my image location.

        if (isFacebookExist()) {


        if (hashClick.isChecked()) {


            SharePhoto sharePhoto = new SharePhoto.Builder()
                    .setBitmap(updateImage)
                    .build();


            if (ShareDialog.canShow(SharePhotoContent.class)) {
                SharePhotoContent content = new SharePhotoContent.Builder()
                        .addPhoto(sharePhoto)
                        .setShareHashtag(new ShareHashtag.Builder()
                                .setHashtag("#HashTag")
                                .build())
                        .build();
                shareDialog.show(content);
            }

        } else {

            SharePhoto sharePhoto = new SharePhoto.Builder()
                    .setBitmap(updateImage)
                    .build();


            if (ShareDialog.canShow(SharePhotoContent.class)) {
                SharePhotoContent content = new SharePhotoContent.Builder()
                        .addPhoto(sharePhoto)
                        .build();
                shareDialog.show(content);
            }

        }


    } else {
        showToast(" Facebook is not install.");
    }


private boolean isFacebookExist() {

    PackageManager pm = getPackageManager();
    try {
        PackageInfo info = pm.getPackageInfo("com.facebook.katana", PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
    return true;
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006564

Could the reason be some security restrictions, e.g. The FB app does not have rights to access the image in the application folder even though it is invoked from an intent?

Correct. That image is on internal storage for your app, which is private to your app.

If so, what would be a proper location for an image shared between the apps?

You can stick with internal storage, though you will need to use a FileProvider, perhaps with my LegacyCompatCursorWrapper, to serve the file. This sample app demonstrates this, albeit with a PDF rather than an image.

Or, put the file on external storage.

Shall I use something like this: how to share image to facebook via intent

You could, though that would seem to be overkill, compared to using FileProvider.

Upvotes: 1

Miriana Itani
Miriana Itani

Reputation: 915

This is what I usually use

    private void initShareIntent(String type) {
    boolean found = false;
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");

    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(type) ||
                    info.activityInfo.name.toLowerCase().contains(type)) {
                share.putExtra(Intent.EXTRA_TEXT, "Elevator Express");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath))); // Optional, just if you wanna share an image.
                share.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found) {
                Toast.makeText(getApplicationContext(), "Facebook does not exist", Toast.LENGTH_SHORT).show();

            return;
        }
        startActivity(Intent.createChooser(share, "Select"));
    }
}

and call it like this :

iniShareIntent("face");

Upvotes: 1

Related Questions