May
1
2012

Status Update with Image from Your Android App to Facebook

In this article, I will try to explain how to update Facebook user wall with image. For this post request, firstly you have to create a Facebook App and select how your app integrates with Facebook. You can create a new app from https://developers.facebook.com/apps/ . After then, following by steps from https://developers.facebook.com/docs/mobile/android/build/ you can prepare your app for post operations to Facebook. Actually, It is too easy send text-based status updates user’s wall and there is a little sample on above link (Step 10).

However we need to give attention a point while sending picture. Today smart phones can take very high quality pictures so uploading high size picture it may take long or connection may drop. In this case, resize picture and compress it may be a simple solution.

Here you are sample code for send status update with image:

Facebook facebook = new Facebook("Your Facebook App ID");
byte[] imageData = null;
Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

Bundle params = new Bundle();
params.putByteArray(Facebook.TOKEN, "User Access Token");
params.putByteArray("picture", imageData);
params.putString("message", "message");
AsyncFacebookRunner asFacebookRunner = new AsyncFacebookRunner(facebook);
asFacebookRunner.request("me/photos", params, "POST",
		new RequestListener() {

			public void onMalformedURLException(
					MalformedURLException e, Object state) {
				Log.d(tag, e.getMessage().toString());
			}

			public void onIOException(IOException e, Object state) {
				Log.d(tag, e.getMessage().toString());
			}

			public void onFileNotFoundException(
					FileNotFoundException e, Object state) {
				Log.d(tag, e.getMessage().toString());
			}

			public void onFacebookError(FacebookError e, Object state) {
				Log.d(tag, e.getMessage().toString());
			}

			public void onComplete(String response, Object state) {
				Log.d(tag, response);
			}
		}, null);

Burak AYDIN

Yazar Hakkında: Burak Aydın

Yorum yaz