Facebook SDK failed to generate preview for user only when opengraphobject.setproperty("og:url") is set

StackOverflow https://stackoverflow.com/questions/21647232

Question

I have an app where I want to share a history in facebook. I don't have a web for my application. I only want to share an image with title and description and if it's posible that when click on the shared site they redirect to google play to download app or to facebook app page.

I look for an answer in most of facebook samples and here and there is something that I'm doing wrong for sure. I made all the facebook app configuration. Key hashes, etc... I create a history in open graph called Publicar a Lugar. Action="Publicar" Object="Lugar".

I've this code

private void OpenShareDialog(){
    Session session = Session.getActiveSession();

    if (session != null) {
        // Check for publish permissions
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

    }

    OpenGraphObject oplugar = OpenGraphObject.Factory.createForPost("namespace:lugar");
    oplugar.setProperty("og:type","namespace:lugar");
    oplugar.setProperty("og:title", LugarCargado.getNombre());
    //oplugar.setProperty("og:image", "http://estaticos.sport.es/resources/jpg/2/1/1391546433412.jpg");
    //oplugar.setProperty("og:url", "http://www.udg.edu/");
    oplugar.setProperty("og:description", LugarCargado.getDescripcion());

    //Bitmap bitmap;    
    //List<Bitmap> bitmaps = new ArrayList<Bitmap>();
    //bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.icoappfacebook2);
    //Log.i(TAG, "Imagen: Height:"+bitmap.getHeight()+ " Width:"+ bitmap.getWidth());
    //bitmaps.add(bitmap);

    //OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
    OpenGraphAction action = OpenGraphAction.Factory.createForPost("namespace:publicar");
    action.setProperty("lugar", oplugar);
    action.setType("namespace:publicar");
    action.setProperty("previewPropertyName", oplugar);

    FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "lugar")
            //.setImageAttachmentsForObject("lugar", bitmaps, true)
            //.setImageAttachmentsForAction(bitmaps,true);
            .build();

    uiHelper.trackPendingDialogCall(shareDialog.present()); 
}

I have two diferent errors debugging in my smartphone:

1- if I uncomment one of this lines

//oplugar.setProperty("og:image", "http://estaticos.sport.es/resources/jpg/2/1/1391546433412.jpg");
//oplugar.setProperty("og:url", "http://www.udg.edu/");

I have an error that says "com.facebook.FacebookException: Failed to generate preview for user.". I can't putt an url

2- If I want to add a custom image. If I uncomment this code

//Bitmap bitmap;    
    //List<Bitmap> bitmaps = new ArrayList<Bitmap>();
    //bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.icoappfacebook2);
    //Log.i(TAG, "Imagen: Height:"+bitmap.getHeight()+ " Width:"+ bitmap.getWidth());
    //bitmaps.add(bitmap);

and

//.setImageAttachmentsForAction(bitmaps,true);

I have a black window and something seems to be restarted...

02-08 15:09:46.500: W/GAV3(15911): Thread[GAThread,5,main]: Service unavailable (code=1), will retry. 02-08 15:09:46.500: I/GAV3(15911): Thread[GAThread,5,main]: No campaign data found. 02-08 15:09:48.482: D/AbsListView(15911): unregisterIRListener() is called 02-08 15:09:48.982: D/AbsListView(15911): onVisibilityChanged() is called, visibility : 4 02-08 15:09:48.982: D/AbsListView(15911): unregisterIRListener() is called 02-08 15:09:51.525: W/GAV3(15911): Thread[Service Reconnect,5,main]: Service unavailable (code=1), using local store. 02-08 15:09:53.778: D/AbsListView(16092): Get MotionRecognitionManager 02-08 15:09:53.778: D/AbsListView(16092): onVisibilityChanged() is called, visibility : 8 02-08 15:09:53.778: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:53.788: D/ProgressBar(16092): setProgress = 0 02-08 15:09:53.788: D/ProgressBar(16092): setProgress = 0, fromUser = false 02-08 15:09:53.788: D/ProgressBar(16092): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000 02-08 15:09:53.828: D/AbsListView(16092): onVisibilityChanged() is called, visibility : 4 02-08 15:09:53.828: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:53.838: D/AbsListView(16092): onVisibilityChanged() is called, visibility : 0 02-08 15:09:53.838: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:53.848: D/libEGL(16092): loaded /system/lib/egl/libEGL_adreno200.so 02-08 15:09:53.858: D/libEGL(16092): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 02-08 15:09:53.858: D/libEGL(16092): loaded /system/lib/egl/libGLESv2_adreno200.so 02-08 15:09:53.858: I/Adreno200-EGL(16092): : EGL 1.4 QUALCOMM build:

AYELDER_AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.023+PATCH[ES]_msm8960_JB_2.5.5_CL3556704_release_ENGG (CL3556704) 02-08 15:09:53.858: I/Adreno200-EGL(16092): Build Date: 05/17/13 Fri 02-08 15:09:53.858: I/Adreno200-EGL(16092): Local Branch: 02-08 15:09:53.858: I/Adreno200-EGL(16092): Remote Branch: quic/jb_2.5.5 02-08 15:09:53.858: I/Adreno200-EGL(16092): Local Patches: 34c9e193f12610d3e68dabd6198d2c4bfbc66974 RB: Update the master

timestamp of the hw_image in rb_texture_update_aliased 02-08 15:09:53.858: I/Adreno200-EGL(16092): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.5.04.02.02.092.023 + 01d3c78 +

e6f0547 + LOCAL_PATCH[ES] 02-08 15:09:53.878: D/OpenGLRenderer(16092): Enabling debug mode 0 02-08 15:09:53.878: D/ProgressBar(16092): updateDrawableBounds: left = 0 02-08 15:09:53.878: D/ProgressBar(16092): updateDrawableBounds: top = 0 02-08 15:09:53.878: D/ProgressBar(16092): updateDrawableBounds: right = 72 02-08 15:09:53.878: D/ProgressBar(16092): updateDrawableBounds: bottom = 72 02-08 15:09:54.058: I/System(16092): Loaded time zone names for en in 217ms (205ms in ICU) 02-08 15:09:54.088: D/AbsListView(16092): onVisibilityChanged() is called, visibility : 0 02-08 15:09:54.088: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:54.168: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:54.178: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:54.188: E/ViewRootImpl(16092): sendUserActionEvent() mView == null 02-08 15:09:54.268: D/AbsListView(16092): unregisterIRListener() is called 02-08 15:09:58.833: W/GAV3(16092): Thread[GAThread,5,main]: Service unavailable (code=1), will retry. 02-08 15:09:58.833: I/GAV3(16092): Thread[GAThread,5,main]: No campaign data found. 02-08 15:10:03.838: W/GAV3(16092): Thread[Service Reconnect,5,main]: Service unavailable (code=1), using local store. 02-08 15:10:10.565: D/AbsListView(16092): unregisterIRListener() is called

and display the previous activity.

With the above code sharedialog shows me the facebook dialog and I can share. But without link and without my image.

Can you help me? I've tried a lot of different things!

it's my smartphone? facebook version?

Do you have a simple sample to share a custom (made for user) image with a description in a share dialog?

Thanks and sorry for my english.

Was it helpful?

Solution

If you don't have a web component, then do not set a url in the og object. If you set a url, facebook will try to go scrape that url, and if the OG metadata scraped from there doesn't match what you've provided, then it will fail.

If you want to add a bitmap to your OG action, in addition to what you're currently doing, you must also add a NativeAppCallContentProvider to your manifest. See the docs here: https://developers.facebook.com/docs/reference/android/current/class/NativeAppCallContentProvider/ This is how the Facebook app can actually get the bitmaps you're trying to send.

Lastly, have a look at the Scrumptious sample app that ships with the sdk. It has an option (if you "skip login") that creates a new OG object, with an image attachment.

OTHER TIPS

With this code it works and I don't have "Failed to generate preview". But this code is working something different that I want. This code open a OGActiondialog but when I share it the post never appear in the feed and appear in the activity registry but only appear the photo, not the name or description like a "normal" sharelink (small photo to the left and titol and description to the right).

Is it possible to do this?

private void OpenShareDialog2(){

    if (canPresentOGShareDialog) {
        Session session = Session.getActiveSession();

        if (session != null) {
            // Check for publish permissions
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest);
                Log.d(TAG, "Permisos pedidos");
                return;
            }

        }

        OpenGraphObject oplugar = OpenGraphObject.Factory.createForPost("namespace:lugar");
        oplugar.setProperty("og:type","namespace:lugar");
        oplugar.setProperty("og:title", LugarCargado.getNombre());
        oplugar.setProperty("og:description", LugarCargado.getDescripcion());



        //OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
        OpenGraphAction action = OpenGraphAction.Factory.createForPost("namespace:publicar");
        action.setProperty("lugar", oplugar);
        action.setType("namespace:publicar");
        //action.setProperty("previewPropertyName", "lugar");
        //action.setProperty("fb:explicitly_shared", true);
        //action.setExplicitlyShared(true);

        boolean userGenerated = false;
        if (urifoto != null) {
            String photoUriString = urifoto.toString();
            Pair<File, Integer> fileAndMinDimemsion = getImageFileAndMinDimension();
            userGenerated = fileAndMinDimemsion.second >= USER_GENERATED_MIN_SIZE;

            // If we have a content: URI, we can just use that URI, otherwise we'll need to add it as an attachment.
            if (fileAndMinDimemsion != null && urifoto.getScheme().startsWith("content")) {
                action.setImage(getImageListForAction(photoUriString, userGenerated));
            }
        }

        FacebookDialog.OpenGraphActionDialogBuilder builder = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "lugar");
                //.setFragment(SelectionFragment.this);

        if (urifoto != null && !urifoto.getScheme().startsWith("content")) {
            builder.setImageAttachmentFilesForAction(Arrays.asList(new File(urifoto.getPath())), userGenerated);
        }

        uiHelper.trackPendingDialogCall(builder.build().present()); 
    }
}

private Pair<File, Integer> getImageFileAndMinDimension() {
    File photoFile = null;
    String photoUriString = urifoto.toString();
    if (photoUriString.startsWith("file://")) {
        photoFile = new File(urifoto.getPath());
    } else if (photoUriString.startsWith("content://")) {
        String [] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = this.getContentResolver().query(urifoto, filePath, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePath[0]);
            String filename = cursor.getString(columnIndex);
            cursor.close();

            photoFile = new File(filename);
        }
    }

    if (photoFile != null) {
        InputStream is = null;
        try {
            is = new FileInputStream(photoFile);

            // We only want to get the bounds of the image, rather than load the whole thing.
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);

            return new Pair<File, Integer>(photoFile, Math.min(options.outWidth, options.outHeight));
        } catch (Exception e) {
            return null;
        } finally {
            Utility.closeQuietly(is);
        }
    }
    return null;
}

I get it more or less from scrumptious sample!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top