Android & Facebook SDK : decoding pictures from /me/picture graph call - android

EDIT: Anwser at the end of this post.
I am trying to get a Facebook user's profile picture thanks to the inbuilt Facebook SDK's function Request().
I am using a /me/picture call to get the profile picture and convert it into a Bitmap.
The call is working fine but I don't know how to interpret and parse the result returned from Facebook
Here is the JSON that I get:
{
"FACEBOOK_NON_JSON_RESULT" : "����\u0000\u0010JFIF\u0000\u0001\u0002\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000\u0004*\u0000��\u0000C\u0000\u0006\u0004\u0005\u0006\u0005\u0004\u0006\u0006\u0005\u0006\u0007\u0007\u0006\b"
}
I think this should be representing the profile picture but I don't know what to do with this now.
If somebody could tell me either how to decode it, convert it to a Bitmap or what kind of data it is, I would be grateful.
NOTES: I don't want to use any other function than Request(), like DecodeStream() or an AsyncTask
Here is the answer:
When making a new Request() you need to add the "redirect" parameter to false:
Bundle params = new Bundle();
params.putBoolean("redirect", false);
This will return the correct picture URL:
{
"data":{
"is_silhouette":false,
"url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xpa1\/v\/t1.0-1\/p100x100\/10312434_10152395442477210_2933994167675146084_n.jpg?oh=f3c8cb920c97fcfa4dc5e17462445cbf&oe=54404A3A&__gda__=1412730795_5f288e34e5f6e63cb1c3791dcb142880"
}
}

Try this :
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);
Where ID is one your profile ID.
For Further details check this :
https://developers.facebook.com/docs/graph-api

Related

Image sent from android to .net rest api is null on the server

I'm building this android app where I need to send images to the server. I'm using retrofit to do so. I get image path as its answer in this stack overflow question Get file path of image on Android and send images as shown here How to upload an image file in Retrofit 2. When I decode a file that I create in the android studio it is valid, but when I send it to my .net rest API it is null. Here is the rest API code, the idea is to get an image if it's null send an image that says it's null, and if not save that image and send it back. The part where I'm sending it back works perfectly, that is I receive an image sent to android, but the image that is sent from android is null.
[Route("post")]
public async Task<IActionResult> PostSlika(IFormFile formFile)
{
if (formFile is null)
{
var stream2 = new FileStream(Path.Combine(_host.WebRootPath, "Images/null_je.jpg"), FileMode.Open);
return File(stream2, "image/jpg");
}
using (var stream = System.IO.File.Create(Path.Combine(_host.WebRootPath, "Images/1.jpg")))
{
await formFile.CopyToAsync(stream);
}
var stream1 = new FileStream(Path.Combine(_host.WebRootPath, "Images/1.jpg"), FileMode.Open);
return File(stream1, "image/jpg");
}
}
I guess there is a conversion problem when converting data to IFormFile. But first check the incomming data with Base64String. I mean:
[Route("post")]
public async Task<IActionResult> PostSlika(string formFileStr)
{
//Convert formFileStr from Base64String to IFormFile
}
Note: you should send Base64String to your service to test it.
If that is OK but you don't want to use this approach then you can use UriTemplate like this:
[WebInvoke( Method= "POST",
//These two parameters are not necessary, but you can use them:
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
//
UriTemplate = "PostSlika/?q={formFile}")]
public async Task<IActionResult> PostSlika(IFormFile formFile)
{
//your codes here
}
Then address to your service method should be changed a little:
{Your Service Address}/PostSlika/?q={object of IFormFile}

How do I upload file using Salesforce Mobile SDK for android?

I am using the Salesforce SDK (4.1.x) in a native Android app. I use the RestClient.sendAsync method to post my form data to a custom object. That part is working fine. Now I need to upload and attach a photo that was taken by the mobile user. I see that RestClient has an uploadFile method. Is this the correct method? If so then how do I connect the uploaded file to the custom form data?
Ok. I figured this out. First, create the parent object (the main form data) using the following.
request = RestRequest.getRequestForCreate(apiVersion, objectType, fields);
client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {...
In the onSuccess method you will get the id of the new object from the response. There are plenty of examples that show how to get the JSON object and the id. Armed with this parentId we can now create the attachment. The code looks something like this.
private void postImageAsAttachment(String parentId, String title) {
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("Name", title);
fields.put("ParentId", parentId);
fields.put("Body", ImageHelper.getBase64FromImage(mCurrentPhotoPath));
RestRequest request = null;
try {
request = RestRequest.getRequestForCreate(apiVersion, "Attachment", fields);
} catch (Exception ex) {
Log.d(TAG, "sendRequest: ", ex);
Toast.makeText(MainActivity.this, "The file upload failed: " + ex.toString(), Toast.LENGTH_LONG).show();
}
client.sendAsync(request, new RestClient.AsyncRequestCallback() {...
I'm using a simple class called ImageHelper that simply loads the image file, performs image compression (if necessary), and base64 encodes the image data. The result is that an "Attachment" object is created as a child of the parent object.
I hope this helps the next person.

User Generated Image for Open Graph from Android

I've been following the tutorial to create an open graph story here. Everything works nicely, but I'd like the uploaded image to be large i.e. "user generated".
My problem is in actually implementing that. Here's the code that gets the image from the SD card:
// If uploading an image, set up the first batch request
// to do this.
// Set up image upload request parameters
Bundle imageParams = new Bundle();
Bitmap image = BitmapFactory.decodeFile(getActivity().getIntent().getStringExtra("image_for_facebook"));
// Set up the image upload request callback
Request.Callback imageCallback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// Log any response error
FacebookRequestError error = response.getError();
if (error != null) {
dismissProgressDialog();
Log.i(TAG, error.getErrorMessage());
}
}
};
// Create the request for the image upload
Request imageRequest = Request.newUploadStagingResourceWithImageRequest(Session.getActiveSession(),
image, imageCallback);
// Set the batch name so you can refer to the result
// in the follow-on object creation request
imageRequest.setBatchEntryName("imageUpload");
// Add the request to the batch
requestBatch.add(imageRequest);
And here's the bit which subsequently gives the graph object its properties:
// Set up the OpenGraphObject representing the book.
OpenGraphObject book = OpenGraphObject.Factory.createForPost("books.book");
// Set up the book image; if we uploaded the image, it is the "uri" result from the previous
// batch request, otherwise it is just a URL.
String imageUrl = "{result=imageUpload:$.uri}";
book.setImageUrls(Arrays.asList(imageUrl));
book.setTitle("A Game of Thrones");
book.setUrl("https://facemash.biz/books/a_game_of_thrones/");
book.setDescription("In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.");
// books.book-specific properties go under "data"
book.getData().setProperty("isbn", "0-553-57340-3");
What do I have to do to set the "user_generated" parameter of the image shown here to be true? (Assuming of course that I'm right in thinking that that's all I have to do to get a large image rather than a thumbnail).
Thanks!
Have a look at the Scrumptious sample, in SelectionFragment.java, the getImageObject method. Basically, you need to create a graph object with the "url" and the "user_generated" properties:
GraphObject imageObject = GraphObject.Factory.create();
imageObject.setProperty("url", "{result=imageUpload:$.uri}");
imageObject.setProperty("user_generated", "true");
GraphObjectList<GraphObject> images = GraphObject.Factory.createList(GraphObject.class);
images.add(imageObject);
book.setImage(images);
Ming's answer came very close for me. In order to attach an image to an OpenGraphAction, I had to do the following:
GraphObject imageObject = GraphObject.Factory.create();
imageObject.setProperty("url", imageUrl);
imageObject.setProperty("user_generated", true);
List<JSONObject> images = new ArrayList<JSONObject>();
images.add(imageObject.getInnerJSONObject());
action.setImage(images);
Open Graph Actions have the method setImage(List<JSONObject> images), but I couldn't find a setImage(GraphObjectList<GraphObject>) method. I'm using Facebook SDK v3.6, so that may have changed.

Android Twitter getting profile image size issue

I'm trying to get the profile images of my followers for use within a ListView as thumbnails.
These thumbnails are around 125x125, but the standard twitter4j call of User.getProfileImageURL(); returns much smaller size of 48x48 and is also recommended to not be used as the image source.
I've tried creating a ProfileImage object and supplying it as a parameter, User.getProfileImageURL(profile image object.Original),
But this code takes some time to simply retrieve the url, which when loading a list of thumbnails, is inefficient.
Any suggestions on how to go about this?
Edit
Twitter API v1 has been disabled, so my old answer is no longer valid. Refer to API v1.1, which I believe requires authentication.
If you know the screen name, the twitter api allows for you to fetch the profile image at 4 different resolutions;
https://api.twitter.com/1/users/profile_image?screen_name=Krylez&size=mini
https://api.twitter.com/1/users/profile_image?screen_name=Krylez&size=normal
https://api.twitter.com/1/users/profile_image?screen_name=Krylez&size=bigger
https://api.twitter.com/1/users/profile_image?screen_name=Krylez&size=original
The "bigger" image is 73x73, which is going to interpolate in your 125x125 container. If you're not okay with this, you can try to fetch the "original" photo, but this photo could be very large (slow) and it's not necessarily a square.
Whatever method you choose, make sure you're not fetching and/or decoding Bitmaps on the UI thread. The Android API documentation has excellent guidelines for the correct way to do this.
Also we can make use of the Twitter4j using:
mTwitter.getUserProfileImage();
From the official doc:
You can obtain a user’s most recent profile image from GET users/show. Within the user object, you’ll find the profile_image_url
and profile_image_url_https fields. These fields will contain the
resized “normal” variant of the user’s uploaded image. This “normal”
variant is typically 48x48px.
By modifying the URL, you can retrieve other variant sizings such as
“bigger”, “mini”, and “original”.
Following the code:
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
twitterApiClient.getAccountService().verifyCredentials(false, false, new Callback<User>() {
#Override
public void success(Result<User> userResult) {
String name = userResult.data.name;
String email = userResult.data.email;
// _normal (48x48px) | _bigger (73x73px) | _mini (24x24px)
String photoUrlNormalSize = userResult.data.profileImageUrl;
String photoUrlBiggerSize = userResult.data.profileImageUrl.replace("_normal", "_bigger");
String photoUrlMiniSize = userResult.data.profileImageUrl.replace("_normal", "_mini");
String photoUrlOriginalSize = userResult.data.profileImageUrl.replace("_normal", "");
}
#Override
public void failure(TwitterException exc) {
Log.d("TwitterKit", "Verify Credentials Failure", exc);
}
});
For further information refer to Twitter API Documentation | Profile Images and Banners
To create a custom size pic, for example 90x90 you can use the createScaledBitmap() method.
private final int PROFILE_PIC_SIZE = 90;
Bitmap originalPic = null;
Bitmap resizedPic = null;
try {
InputStream in = new java.net.URL(photoUrlOriginalSize).openStream();
originalPic = BitmapFactory.decodeStream(in);
resizedPic = Bitmap.createScaledBitmap(originalPic, PROFILE_PIC_SIZE, PROFILE_PIC_SIZE, false);
} catch (Exception exc) {
Log.e("Error", exc.getMessage());
exc.printStackTrace();
}
You can use getOriginalProfileImageURL() for example. This is as large as it gets.
Smaller ones are getBiggerProfileImageURL() and getProfileImageURL().
These are the urls you retrieve:
http://pbs.twimg.com/profile_images/NUMBER/c62p-cAD_normal.jpeg
http://pbs.twimg.com/profile_images/NUMBER/c62p-cAD_bigger.jpeg
http://pbs.twimg.com/profile_images/NUMBER/c62p-cAD.jpeg

Delete image from facebook album

I'm developing app which post images to facebook. That's working quite good. But my problem is that i can't find sample that would delete selected photo from facebook. I have allready made an function that gets the WallId and PhotoID from facebook.
My problem is that i can't get what should i put in facebook runner.request i know that i need to pass DELETE and PhotoId my function currently is
private void delete_fb(){
AsyncFacebookRunner fruner = new AsyncFacebookRunner(facebook);
Bundle params = new Bundle();
params.putString("fields", "id,name");
if(favi.get(indeks).fbid!=0 && WallId != 0){
fruner.request("me/"+WallId+"/"+favi.get(index).fbid,params ,"DELETE",new Del(),null);
}
}
So if you could explain me what to put in place of "me/"+WallId+"/"+favi.get(index).fbid in fruner.request() or give me some example code i would be very pleased.
Thank you.
try this
fruner.request(YourPostId, params ,"DELETE", new Del(), null);

Categories

Resources