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));
}
}
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);
}
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);
}
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);
}
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());
}
}
Aggregations