朋友个人资料图片与 android 中 facebook 的个人资料名称不匹配

Posted

技术标签:

【中文标题】朋友个人资料图片与 android 中 facebook 的个人资料名称不匹配【英文标题】:Friends profile picture is not matching with the profile name for facebook in android 【发布时间】:2014-06-21 09:48:59 【问题描述】:

我正在从 Facebook 获取朋友姓名、生日和个人资料图片。我正在列表视图中显示,但个人资料图片与那里的姓名不匹配。 我试过下面的代码:

 public void onComplete(String response, Object state) 

   Log.v("", "FriendListRequestONComplete");
  friendData = response;
 Log.v("friendData--", ""+friendData);

 //Create method to run on UI thread
   MainActivity.this.runOnUiThread(new Runnable() 
   @SuppressLint("NewApi")
   public void run() 

 try 
   //Parse JSON Data
 //  pick(userID);

  JSONObject json;
   //json = Util.parseJson(friendData);
  json = new JSONObject(friendData);


   //Get the JSONArry from our response JSONObject
    friendArray = json.getJSONArray("data");


   Log.v("friendArray--", ""+friendArray);
   for(i = 0; i< friendArray.length(); i++)
   
      frnd_obj = friendArray.getJSONObject(i);

    try
                  friends.add("Name:"+frnd_obj.getString("name")+"\n"+"DOB:"+frnd_obj.getString("birthday"));


     String userProfileID=frnd_obj.getString("id");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 

                new DownloadImageTask(img).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "https://graph.facebook.com/"+userProfileID+"/picture?type=small");

             else


             new DownloadImageTask(img).execute("https://graph.facebook.com/"+userProfileID+"/picture?type=small");

            


       
       catch(Exception e) 

           //friends.add("Name:"+frnd_obj.getString("name")); 

      

   list1.setAdapter(new lsAdapter(MainActivity.this));



   catch (JSONException e) 
     // TODO Auto-generated catch block
     e.printStackTrace();
   catch (FacebookError e) 
     // TODO Auto-generated catch block
     e.printStackTrace();
  
 
 );

对于加载配置文件图片 Asyntask:

  private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> 

private ProgressDialog mDialog;
 private ImageView bmImage;
// Bitmap mIcon11 = null;

 public DownloadImageTask(ImageView bmImage) 
     this.bmImage = bmImage;
 

 protected void onPreExecute() 

      mDialog = ProgressDialog.show(MainActivity.this,"Please wait...", "Retrieving data ...", true);
     mDialog.show();
 

 protected Bitmap doInBackground(String... urls) 
     Log.d("image", "do in");
     String urldisplay = urls[0];

     try 
         Log.d("image", "do 1");

         InputStream in = new java.net.URL(urldisplay).openStream();
         mIcon11 = BitmapFactory.decodeStream(in);
         Log.d("image", "do 2");
      catch (Exception e) 
         Log.e("Error", "image download error");
         Log.e("Error", e.getMessage());
       //  mIcon11=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
         e.printStackTrace();
         Log.d("image", "do catch");
     
     Log.d("image", "do out");
     return mIcon11;
 

 protected void onPostExecute(Bitmap result) 
     //set image of your imageview
     Log.d("image", "post");
  //   bmImage.setImageResource(R.drawable.ic_launcher);
     bmImage.setImageBitmap(null);
     bmImage.setVisibility(View.INVISIBLE);
     bmImage.setImageBitmap(result);

     if(result!=null)
         //Toast.makeText(getApplicationContext(), "success", 5000).show();
         mIcon11=result;

     else 
         //Toast.makeText(getApplicationContext(), "Not success", 5000).show();
         mIcon11=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    
    // bmImage.setImageBitmap(mIcon11);
     bitmapArray.add(mIcon11);

     mDialog.dismiss();

     //close
     //mDialog.dismiss();
 
 

这是 BaseAdapter 类:

  class lsAdapter extends BaseAdapter
Context context;
public lsAdapter(Context c)
    context=c;


@Override
public int getCount() 
    // TODO Auto-generated method stub
    //return friends.size();
    return bitmapArray.size();

@Override
public Object getItem(int position) 
    // TODO Auto-generated method stub
    return position;

@Override
public long getItemId(int position) 
    // TODO Auto-generated method stub
    return position;

@Override
public View getView(int position, View v, ViewGroup group) 
    // TODO Auto-generated method stub
    LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    View vi=inflater.inflate(R.layout.customlist, null);
    ImageView iv=(ImageView)vi.findViewById(R.id.iv);
    ImageView next=(ImageView)vi.findViewById(R.id.nextimg);
    TextView tv=(TextView)vi.findViewById(R.id.tv);

        //iv.setImageURI(friendArray.getJSONObject(i).getString("id"));
   tv.setText(friends.get(position));
    iv.setImageBitmap(bitmapArray.get(position));
    return vi;

  

请帮助我。获取正确名称的朋友头像。

【问题讨论】:

【参考方案1】:

看来您只是在 onPostExecute 中添加到 bitmapArray。但是由于 ImageDownloadTasks 是异步执行的,因此无法保证 onPostExecute 何时运行(稍后添加的图像可能会更早完成加载)。这可能就是您看到随机排序的原因。

尝试使用 HashMap,而不仅仅是 bitmapArray,键是“id”,值是位图。然后您可以根据用户 ID 在您的适配器中进行查找。或者,当您创建 ImageDownloadTask 时,为其分配一个位置,并将位图设置在数组中的正确位置。

【讨论】:

以上是关于朋友个人资料图片与 android 中 facebook 的个人资料名称不匹配的主要内容,如果未能解决你的问题,请参考以下文章

Android仿微信朋友圈图片查看器

查询输出与 if 条件不一致

类似微信朋友圈,点击+号图片,可以加图片

Android 调用系统分享文字图片文件,可直达微信朋友圈QQQQ空间微博

Android 实现朋友圈有图片和视频

Android 仿微信朋友圈点击图片变暗