gpt4 book ai didi

android - 如何从Facebook获取图像并在 ImageView 中显示

转载 作者:行者123 更新时间:2023-11-30 01:57:09 25 4
gpt4 key购买 nike

loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
if (user != null) {
userName.setText("Hello, " + user.getName());

Bitmap bitmap1 = getFacebookProfilePicture(user.getId());

imgview.setImageBitmap(bitmap1);
} else {
userName.setText("You are not logged");
}
}
});
public Bitmap getFacebookProfilePicture(String userID) {

try {
URL imageURL = new URL("https://graph.facebook.com/" + userID
+ "/picture?type=large");
bitmap = BitmapFactory.decodeStream(imageURL.openConnection()
.getInputStream());

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;

}

这是我的代码,我正在尝试从 Facebook 获取个人资料图片并将其显示在我的应用程序的 ImageView 中,我能够从 Facebook 获取姓名,但是当我输入获取个人资料图片的代码时,我无法获取图像在 android 中请告诉我哪里做错了给我解决方案如何设置此代码。

最佳答案

使用该方法从Url中获取Bitmap。

 /**
* Download image from server url and return bitmap
*
* @param stringUrl Imaage download url
* @return Bitmap receieved from server
*/
private Bitmap downloadImage(String stringUrl) {
URL url;
Bitmap bm = null;
try {
url = new URL(stringUrl);
URLConnection ucon = url.openConnection();
InputStream is;
if (ucon instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) ucon;
int statusCode = httpConn.getResponseCode();
if (statusCode == 200) {
is = httpConn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
BufferedInputStream bis = new BufferedInputStream(is, 8192);
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] rawImage = baf.toByteArray();
bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
bis.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}

像这样将 Facebook 个人资料图片 url 传递给此方法,并将下载的位图设置为 imageview。

 URL imageURL = new URL("https://graph.facebook.com/" + userID
+ "/picture?type=large");

imgview.setImageBitmap(downloadImage(imageURL));

希望对您有所帮助!

关于android - 如何从Facebook获取图像并在 ImageView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com