[英]ImageView get blanked after loading from Facebook using social-auth android
我正在嘗試使用social-auth android庫從Facebook獲取用戶個人資料照片。
我已經成功登錄,在ResponseListener的onComplete上登錄后,我得到了用戶信息,但是我獲得了用戶配置文件圖像的url,但是當我將其設置為ImageView時,我的ImageView卻變為空白。
這是我的代碼。
mSocialAdapter = new SocialAuthAdapter(new ResponseListener());
mSocialAdapter.authorize(this,Provider.FACEBOOK);
private final class ResponseListener implements DialogListener{
@Override
public void onBack() {
}
@Override
public void onCancel() {
}
@Override
public void onComplete(Bundle bundle) {
mSocialAdapter.getUserProfileAsync(new SocialAuthListener<Profile>() {
@Override
public void onExecute(String arg0, Profile profile) {
if(profile.getDisplayName() != null)
Log.e("Display name", profile.getDisplayName());
if(profile.getProfileImageURL() != null){
Log.e("Profile Image Url", profile.getProfileImageURL());
Picasso.with(RegisterProfileSetupActivity.this).load(profile.getProfileImageURL()).into(imgUserPhoto);
//imgUserPhoto.setImageBitmap(loadImage(profile.getProfileImageURL()));
}
}
@Override
public void onError(SocialAuthError arg0) {
}
});
}
@Override
public void onError(SocialAuthError arg0) {
}
}
我也嘗試這種方法來加載位圖
public Bitmap loadImage(String url) {
DefaultHttpClient client = new DefaultHttpClient();
Bitmap bitmap = null;
try {
HttpResponse response = client.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
if(entity != null) {
InputStream in = entity.getContent();
bitmap = BitmapFactory.decodeStream(in);
}
}
catch (Exception e) {
}
return bitmap;
}
實際上,您需要在AsyncTask類中異步加載圖像。 這是代碼。 工作正常。
private static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client =
AndroidHttpClient.newInstance("Android");
final HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
final int statusCode =
response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length != 0) {
String newUrl =
headers[headers.length - 1].getValue();
// call again with new URL
return downloadBitmap(newUrl);
} else {
return null;
}
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
return BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
request.abort();
} finally {
if (client != null) {
client.close();
}
}
return null;
}
class LoadImageTask extends AsyncTask<String, Void, Bitmap>
{
@Override
protected void onPreExecute() {
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap btn = downloadBitmap(params[0]);
return btn;
}
@Override
protected void onPostExecute(Bitmap result) {
imgProfileImage.setImageBitmap(result);
// super.onPostExecute(result);
}
}
代替
//imgUserPhoto.setImageBitmap(loadImage(profile.getProfileImageURL()));
寫這行
new LoadImageTask().execute(profile.getProfileImageURL());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.