我正在研究Android项目,我需要从Facebook获取图像并在UI上显示。我能够获取我需要执行的Facebook Image的URL,以便我可以获取图像。
以下是我需要执行以获取我的脸书图片的网址以及我从userList.get(position).getPhoto()
获得的以下网址。
http://graph.facebook.com/fb-user-id/picture
下面是我用于在后台获取图像的代码,但不知何故我的后台任务没有被调用,Facebook图像没有显示在我的UI上。
也没有错误。
有没有更好的方法来获取图像并在UI上显示?
/**
* Depending on the gender, set the image properly for the users
*/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.item, null);
}
TextView text = (TextView) vi.findViewById(R.id.text);
TextView text1 = (TextView) vi.findViewById(R.id.text1);
ImageView image = (ImageView) vi.findViewById(R.id.image);
ImageView genderImage = (ImageView) vi.findViewById(R.id.image1);
text.setText("" + userList.get(position).getFirstName() + " "
+ userList.get(position).getLastName());
text1.setText("" + userList.get(position).getDistance() + " meter");
if (userList.get(position).getGender() == 1) {
genderImage.setImageResource(R.drawable.male_sign);
} else {
genderImage.setImageResource(R.drawable.female_sign);
}
//Calling background task to get the image but somehow, it's not working
new DownloadImageTask(image).execute(userList.get(position).getPhoto());
return vi;
}
下面是将在后台获取图像然后在UI上显示它的类。
/**
* A simple class to do the task in background so that it can download the image without blocking the main thread
*
*/
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
答案 0 :(得分:1)
您是否尝试将(ImageView)图像设为全局变量?
不需要在DownloadImageTask中发送它,只需
image.setImageBitmap(result);
在我的代码中,我将其设置在 onProgressUpdate
中