Search in sources :

Example 11 with SharePhoto

use of com.facebook.share.model.SharePhoto in project facebook-android-sdk by facebook.

the class HelloFacebookSampleActivity method postPhoto.

private void postPhoto() {
    Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
    SharePhoto sharePhoto = new SharePhoto.Builder().setBitmap(image).build();
    ArrayList<SharePhoto> photos = new ArrayList<>();
    photos.add(sharePhoto);
    SharePhotoContent sharePhotoContent = new SharePhotoContent.Builder().setPhotos(photos).build();
    if (canPresentShareDialogWithPhotos) {
        shareDialog.show(sharePhotoContent);
    } else if (hasPublishPermission()) {
        ShareApi.share(sharePhotoContent, shareCallback);
    } else {
        pendingAction = PendingAction.POST_PHOTO;
        // We need to get new permissions, then complete the action when we get called back.
        LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList(PERMISSION));
    }
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) Bitmap(android.graphics.Bitmap) SharePhoto(com.facebook.share.model.SharePhoto) ArrayList(java.util.ArrayList)

Example 12 with SharePhoto

use of com.facebook.share.model.SharePhoto in project facebook-android-sdk by facebook.

the class ShareContentValidationTest method testItValidatesNullImageForPhotoShareByMessage.

// -PhotoContent
@Test(expected = FacebookException.class)
public void testItValidatesNullImageForPhotoShareByMessage() {
    SharePhotoContent.Builder spcBuilder = new SharePhotoContent.Builder();
    SharePhoto sharePhoto = new SharePhoto.Builder().setImageUrl(null).setBitmap(null).build();
    SharePhotoContent sharePhotoContent = spcBuilder.addPhoto(sharePhoto).build();
    ShareContentValidation.validateForMessage(sharePhotoContent);
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) SharePhoto(com.facebook.share.model.SharePhoto) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with SharePhoto

use of com.facebook.share.model.SharePhoto in project facebook-android-sdk by facebook.

the class ShareContentValidationTest method testItDoesAcceptSharePhotoContentByWeb.

@Test
public void testItDoesAcceptSharePhotoContentByWeb() {
    SharePhoto sharePhoto = buildSharePhoto("https://facebook.com/awesome.gif");
    SharePhotoContent sharePhotoContent = new SharePhotoContent.Builder().addPhoto(sharePhoto).build();
    ShareContentValidation.validateForWebShare(sharePhotoContent);
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) SharePhoto(com.facebook.share.model.SharePhoto) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with SharePhoto

use of com.facebook.share.model.SharePhoto in project facebook-android-sdk by facebook.

the class RequestTests method testExecuteUploadPhotoToAlbum.

@LargeTest
public void testExecuteUploadPhotoToAlbum() throws InterruptedException, JSONException {
    // first create an album
    Bundle params = new Bundle();
    params.putString("name", "Foo");
    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/albums", params, HttpMethod.POST);
    GraphResponse response = request.executeAndWait();
    JSONObject jsonResponse = response.getJSONObject();
    assertNotNull(jsonResponse);
    String albumId = jsonResponse.optString("id");
    assertNotNull(albumId);
    // upload an image to the album
    Bitmap image = createTestBitmap(128);
    SharePhoto photo = new SharePhoto.Builder().setBitmap(image).setUserGenerated(true).build();
    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
    final ShareApi shareApi = new ShareApi(content);
    shareApi.setGraphNode(albumId);
    final AtomicReference<String> imageId = new AtomicReference<>(null);
    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            shareApi.share(new FacebookCallback<Sharer.Result>() {

                @Override
                public void onSuccess(Sharer.Result result) {
                    imageId.set(result.getPostId());
                    notifyShareFinished();
                }

                @Override
                public void onCancel() {
                    notifyShareFinished();
                }

                @Override
                public void onError(FacebookException error) {
                    notifyShareFinished();
                }

                private void notifyShareFinished() {
                    synchronized (shareApi) {
                        shareApi.notifyAll();
                    }
                }
            });
        }
    });
    synchronized (shareApi) {
        shareApi.wait(REQUEST_TIMEOUT_MILLIS);
    }
    assertNotNull(imageId.get());
    // now check to see if the image is in the album
    GraphRequest listRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), albumId + "/photos");
    GraphResponse listResponse = listRequest.executeAndWait();
    JSONObject listObject = listResponse.getJSONObject();
    assertNotNull(listObject);
    JSONArray jsonList = listObject.optJSONArray("data");
    assertNotNull(jsonList);
    boolean found = false;
    for (int i = 0; i < jsonList.length(); i++) {
        JSONObject imageObject = jsonList.getJSONObject(i);
        if (imageId.get().equals(imageObject.optString("id"))) {
            found = true;
        }
    }
    assertTrue(found);
}
Also used : SharePhotoContent(com.facebook.share.model.SharePhotoContent) Bundle(android.os.Bundle) JSONArray(org.json.JSONArray) AtomicReference(java.util.concurrent.atomic.AtomicReference) Bitmap(android.graphics.Bitmap) JSONObject(org.json.JSONObject) SharePhoto(com.facebook.share.model.SharePhoto) Sharer(com.facebook.share.Sharer) ShareApi(com.facebook.share.ShareApi) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 15 with SharePhoto

use of com.facebook.share.model.SharePhoto in project facebook-android-sdk by facebook.

the class GraphRequest method createOpenGraphObject.

/**
     * Create an User Owned Open Graph object
     *
     * Use this method to create an open graph object, which can then be posted utilizing the same
     * GraphRequest methods as other GraphRequests.
     *
     * @param openGraphObject The open graph object to create. Only SharePhotos with the imageUrl
     *                        set are accepted through this helper method.
     * @return GraphRequest for creating the given openGraphObject
     * @throws FacebookException thrown in the case of a JSONException or in the case of invalid
     *                           format for SharePhoto (missing imageUrl)
     */
public static GraphRequest createOpenGraphObject(final ShareOpenGraphObject openGraphObject) throws FacebookException {
    String type = openGraphObject.getString("type");
    if (type == null) {
        type = openGraphObject.getString("og:type");
    }
    if (type == null) {
        throw new FacebookException("Open graph object type cannot be null");
    }
    try {
        JSONObject stagedObject = (JSONObject) OpenGraphJSONUtility.toJSONValue(openGraphObject, new OpenGraphJSONUtility.PhotoJSONProcessor() {

            @Override
            public JSONObject toJSONObject(SharePhoto photo) {
                Uri photoUri = photo.getImageUrl();
                JSONObject photoJSONObject = new JSONObject();
                try {
                    photoJSONObject.put(NativeProtocol.IMAGE_URL_KEY, photoUri.toString());
                } catch (Exception e) {
                    throw new FacebookException("Unable to attach images", e);
                }
                return photoJSONObject;
            }
        });
        String ogType = type;
        Bundle parameters = new Bundle();
        parameters.putString("object", stagedObject.toString());
        String graphPath = String.format(Locale.ROOT, GRAPH_PATH_FORMAT, ME, "objects/" + ogType);
        return new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, parameters, HttpMethod.POST);
    } catch (JSONException e) {
        throw new FacebookException(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) SharePhoto(com.facebook.share.model.SharePhoto) JSONException(org.json.JSONException) Uri(android.net.Uri) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Aggregations

SharePhoto (com.facebook.share.model.SharePhoto)17 Bitmap (android.graphics.Bitmap)6 SharePhotoContent (com.facebook.share.model.SharePhotoContent)5 ShareOpenGraphAction (com.facebook.share.model.ShareOpenGraphAction)4 ArrayList (java.util.ArrayList)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Uri (android.net.Uri)3 LargeTest (android.test.suitebuilder.annotation.LargeTest)3 ShareOpenGraphObject (com.facebook.share.model.ShareOpenGraphObject)3 Bundle (android.os.Bundle)2 Sharer (com.facebook.share.Sharer)2 ShareOpenGraphContent (com.facebook.share.model.ShareOpenGraphContent)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 JSONArray (org.json.JSONArray)2 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 FacebookException (com.facebook.FacebookException)1