GraphAPIを使用してタグ付けされた画像をFacebookにアップロードしています。これが私のコードです:
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
HttpClient httpClient = new DefaultHttpClient();
mPostTagUrl = "https://graph.facebook.com/"
+ dataManager.getOwnerId() + "/photos";
HttpPost httppost = new HttpPost(mPostTagUrl);
byte[] data = null;
try {
try {
String mPath = Environment.getExternalStorageDirectory()
.toString() + "/" + "screenshot.jpg";
FileInputStream fis = new FileInputStream(mPath);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("onCreate", "debug error e = " + e.toString());
}
if (data != null) {
ByteArrayBody bab = new ByteArrayBody(data, "event_pic"
+ ".jpg");
entity.addPart("source", bab);
}
StringBody mAccesssToken = new StringBody(dataManager
.getAccessToken().trim());
entity.addPart("access_token", mAccesssToken);
JSONArray mJsonArray = new JSONArray();
JSONObject json;
for (int i = 0; i < getAllAddFriends().size(); i++) {
final int index = i;
String fId = getAllAddFriends().get(index)
.getFacebookFriendId();
Log.d("gaurav", "facebook id ==" + fId);
try {
json = new JSONObject();
json.put("x", "" + position);
json.put("y", "" + position);
json.put("tag_uid", fId);
position = position + 10;
mJsonArray.put(i, json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String str = new String(mJsonArray.toString());
Log.d("gaurav", "Add event Url=" + str);
StringBody mEventNameBody = new StringBody(str);
entity.addPart("tags", mEventNameBody);
httppost.setEntity(entity);
try {
httpClient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse response = httpClient.execute(httppost);
String ret = EntityUtils.toString(response.getEntity());
Log.d("gaurav", "Response Tag Friends =" + ret);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
親切に私のコードを確認してください。タグ付きの画像を投稿することはできますが、Facebookでは同じ画像を同時に2回アップロードします。その問題について私に提案してください。