我正在尝试将从 Facebook 返回的个人资料图像转换为位图,以便我可以在需要位图的方法中使用它。我可以使用下面的代码获取个人资料图片并将其放入 ProfilePictureView 中。我还创建了另外两个 View 用于测试......一个 CircleImageView 和一个 ImageView。到目前为止,只有 ProfilePictureView 显示图像。这是我尝试过的代码示例,尽管我尝试了在网上找到的几种创建位图的方法,但没有一个有效。任何建议/解决方案将不胜感激!
ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.facebookPictureView);
profilePictureView.setProfileId(userId); //this works to get and set the profile pic
standardImageView = (ImageView) findViewById(R.id.imageView);
circleImageView = (CircleImageView) findViewById(R.id.profImage);
AsyncTask<Void, Void, Bitmap> t = new AsyncTask<Void, Void, Bitmap>() {
protected Bitmap doInBackground(Void... p) {
Bitmap bmp = null;
try {
URL aURL = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");
URLConnection conn = aURL.openConnection();
conn.setUseCaches(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bmp = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap bmp) {
standardImageView.setImageBitmap(bmp);
circleImageView.setImageBitmap(bmp);
}
};
t.execute();
使用调试器并逐步执行,我发现 bmp 为空。我尝试了其他几种方法,但也发生了同样的事情。帮助!
最佳答案
您也可以决定使用 Glide,这是 Picasso 的更好选择
Bitmap theBitmap = null;
theBitmap = Glide.
with(getActivigetApplicationContext()).
load("your image url here").
asBitmap().
into(-1, -1).
get();
关于android - 将 Facebook ProfilePictureView 转换为位图以在 ImageView 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32857367/