Looking for android Facebook SDK examples

43,509

Solution 1

I asked a similar question a few weeks ago regarding the official Facebook Android SDK and posting content to one's wall (Android/Java -- Post simple text to Facebook wall?). That should help you get a feel for what it's like to post text to one's wall. I should point out though that you need to first create a Fackbook app and apply for an API key from Fackbook.com (https://kunukd.com/)...if it asks about the platform of the app you intent to create, choose mobile.

You can modify the code in the Stack Overflow link I posted (above) to post photos too. At present though, according to the official git page for the Facebook Android SDK (under "Known Issues"):

3.Binary API parameters (such as uploading pictures) is not yet supported -- coming soon...

So, while you can post a photo to your wall if you have the URL of the image file (the file must already be on the Internet), you can't use this SDK to send binary/byte data of the photo from the Android device (yet... as of 07/24/10). At least, that's what I gather from the statement above.

Replace the following lines of the sample code I posted in the other Stack Overflow post (link above):

Bundle parameters = new Bundle();
parameters.putString("message", "this is a test");// the message to post to the wall
facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call

with this

Bundle parameters = new Bundle();
parameters.putString("message", "Test Photo");
parameters.putString("attachment", "{\"name\":\"My Test Image\","
+"\"href\":\""+"http://www.google.com"+"\","
+"\"media\":[{\"type\":\"image\",\"src\":\""+"http://www.google.com/logos/mucha10-hp.jpg"+"\",\"href\":\""+"http://www.google.com"+"\"}]"
+"}");
facebookClient.dialog(this, "stream.publish", parameters, this);

and you should be able to post photos to your wall (as well as text and links).

For more help on structuring the "attachment" string, go here: http://www.mobisoftinfotech.com/blog/android/845/.

Other than that, consider using a third-party package or wait for the official SDK to be updated if you need to post photos to an album directly from the device.

Solution 2

You can do it this way:

byte[] data = null;
try {
    ContentResolver cr = mainActivity.getContentResolver();
    InputStream fis = cr.openInputStream(localSnapshotUri);
    Bitmap bi = BitmapFactory.decodeStream(fis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();              
} catch (FileNotFoundException e) {
    e.printStackTrace();
}     

Bundle params = new Bundle(); 
params.putString("method", "photos.upload");          
params.putByteArray("picture", data);

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener());

Parameters used here are:

  • localSnapshotUri which points to some image in you /sdcard/.. or wherever it is :)
  • mainActivity that is app main activity
  • SampleUploadListener an implemetation of AsyncFacebookRunner.RequestListener interface

Have a nice programming!

Solution 3

Just posted here the simple way to upload a photo:

android facebook publish photo

Code:

byte[] data = null;

Bitmap bi = BitmapFactory.decodeFile(photoToPost);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();

Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

Solution 4

By far the easiest and working example of posting to user's wall without the dialog once logged on, and using the new Facebook SDK, is: http://www.integratingstuff.com/2010/10/14/integrating-facebook-into-an-android-application/

By the way, posting images is done with:

parameters.putString("picture", "http://www.google.com/logos/mucha10-hp.jpg"); 
Share:
43,509
brybam
Author by

brybam

Brinkbit.com

Updated on July 15, 2022

Comments

  • brybam
    brybam almost 2 years

    I've looked all over the internet and can't seem to find what i'm looking for...

    I'm just trying to find a site with some examples on how to use the OFFICIAL facebook android SDK http://github.com/facebook/facebook-android-sdk

    To be specific i'd like to see some examples on notification use and simple photo upload. But i'll take anything to help get a feel for using the SDK.

    If anyone knows of any examples please share thank you so much!

  • brybam
    brybam almost 14 years
    That will definitely help so much! Thanks you! And also im curious if you might know the answer to this, Does the SDk maybe have like a "Notifications" class that maybe i could just reference to with a status bar notification and get it to check say like if messages > 1 blah blah. Sorry im really new to this. But this has all been really helpful
  • OneWorld
    OneWorld over 13 years
    How stupid. I thought I can pack attachment into another bundle and make parameters.putBundle(attachmentBundle); But that leads to NullPointerException in the Util.encodeUrl(Bundle parameters) I recommend to use developer.android.com/reference/org/json/JSONObject.html instead.
  • Anthony Graglia
    Anthony Graglia over 13 years
    Thanks. Will this still upload even if the application has been closed?
  • Rocker
    Rocker over 12 years
    Great work boy....Really awesome.....thanks a lot....
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    what is SampleUploadListener() ?
  • Anthony Graglia
    Anthony Graglia over 12 years
    Part of the example provided by the Facebook SDK.
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    Ok Thanks. I got it. But while i am using your code it simple said that, Host is unresolved.
  • Anthony Graglia
    Anthony Graglia over 12 years
    It is probably best to ask a new question and show the code you are using. There needs to be more detail in order to know what you are doing.
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    I have post the Question on the Stackoverflow. So please help me for it.
  • osayilgan
    osayilgan over 10 years
    "picture" is for sharing the image urls.
  • Mehul Joisar
    Mehul Joisar over 10 years
    It works but gives exception about expecting byte array instead of url of the image.