これには、次の 5 つの手順が含まれます。
ステップ1:
Facebook にログイン:
private void loginToFacebook()
{
mPrefs = activity.getPreferences(Context.MODE_PRIVATE);
facebook = new Facebook(APP_ID);
accessToken = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (accessToken != null)
{
facebook.setAccessToken(accessToken);
uploadPhoto(facebook, imageBitmap, null);
}
if (expires != 0)
{
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid())
{
facebook.authorize(activity, new String[] {"publish_stream"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
public void onFacebookError(FacebookError arg0) {
// TODO Auto-generated method stub
}
public void onError(DialogError arg0) {
// TODO Auto-generated method stub
}
public void onComplete(Bundle arg0) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
uploadPhoto(facebook, imageBitmap, null);
}
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
}
ステップ2:
画像を Facebook にアップロードします (ビットマップがアップロードされます)。このメソッドは login メソッド内で呼び出されることに注意してください。Following メソッドはアップロードされた画像の ID を返すため、これは友達にタグを付けるときに必要です。
public String uploadPhoto(Facebook facebook, Bitmap image, String albumId)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if ( albumId == null ) {
albumId = "me";
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.JPEG, 75, bos);
byte[] photoBytes = bos.toByteArray();
Bundle params = new Bundle();
params.putString(facebook.TOKEN, accessToken);
params.putByteArray("picture", photoBytes);
try {
@SuppressWarnings("deprecation")
String resp = facebook.request(albumId + "/photos", params, "POST");
JSONObject json = Util.parseJson(resp);
if(json.getString("id") != null){
Toast.makeText(activity, "Image Successfully uploaded to facebook!", Toast.LENGTH_SHORT).show();
tagFriends();
}else{
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
}
uploadedImageID = json.getString("id");
return json.getString("id");
} catch ( IOException e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
} catch ( FacebookError e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
} catch ( JSONException e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
}
return null;
}
ステップ 3:
Facebook アカウントのすべての友達を取得し、ハッシュ マップに保存します。
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
mAsyncRunner.request("me/friends" , params , "GET", new RequestListener()
{
@Override
public void onMalformedURLException(MalformedURLException e, Object state)
{
}
@Override
public void onIOException(IOException e, Object state)
{
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state)
{
}
@Override
public void onFacebookError(FacebookError e, Object state)
{
}
@Override
public void onComplete(String response, Object state)
{
try
{
JSONObject responseJsonObject = new JSONObject(response);
Log.d("InviteFriends.inviteFriends().new RequestListener() {...}:onComplete", "FB Response =>"+ responseJsonObject);
//event_id = event.getString("id");
//JSONObject responseJsonObject = new JSONObject(eventResponse);
JSONArray jsonArray = responseJsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jsonArray.getJSONObject(i);
map.put("id", e.getString("id"));
map.put("name", e.getString("name"));
mylist.add(map);
userIds = e.getString("id");
userName = e.getString("name");
Log.d("MainActivity:getAllEvents", "Friend ID, Name:" + userIds + "," + userName);
}
}
catch (Exception e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
ステップ 4:
リストビューに名前のリストを表示し、ユーザーがリストからタッチしたユーザー名のユーザー ID を取得して配列に保存します。
final ListView friends = (ListView) findViewById(R.id.lst_frnds);
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), mylist , R.layout.friends_main, new String[] {"name" }, new int[] {R.id.item_title});
Log.d( "size","Friends->" + mylist.size());
friends.setAdapter(adapter);
friends.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) friends.getItemAtPosition(position);
String selectedName = o.get("name");
String selectedId = o.get("id");
Toast.makeText(MemeList.this, "Your friend " + selectedName + "' was selected.", Toast.LENGTH_SHORT).show();
selectedFriendsIDs.add(selectedId);
selectedFriendsNames.add(selectedName);
}
ステップ 5:
リストから選択した友達にタグを付ける:
btnSendInvites.setText(selectedFriendsIDs.size() + " selected, tag them now?");
btnSendInvites.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Tagging friends
for(int i = 0; i < selectedFriendsIDs.size(); i++){
String inputParm = uploadedImageID + "/tags?to=" + selectedFriendsIDs.get(i);
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
mAsyncRunner.request(inputParm, params, "POST", new RequestListener()
{
@Override
public void onMalformedURLException(MalformedURLException e, Object state)
{
}
public void onIOException1(IOException e, Object state)
{
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state)
{
}
@Override
public void onFacebookError(FacebookError e, Object state)
{
}
@Override
public void onComplete(String response, Object state)
{
Log.d("Selected IDs ", "" + selectedFriendsIDs);
}
@Override
public void onIOException(IOException e, Object state)
{
// TODO Auto-generated method stub
}
}, null);
}
}
});
タグ付けに成功すると、true が返されます。
これが誰かを助けることを願っています!