Android 将Facebook ProfilePictureView转换为位图以在ImageView中使用
我正在尝试将从Facebook返回的个人资料图像转换为位图,以便在需要位图的方法中使用它。我可以通过使用下面的代码获得配置文件图片,并将其放入ProfilePictureView。我还创建了另外两个用于测试的视图…CircleImageView和ImageView。到目前为止,只有ProfilePictureView显示了该图像。下面是我尝试过的代码示例,尽管我尝试过在web上找到的几种创建位图的方法,但都没有成功。如有任何建议/解决方案,将不胜感激Android 将Facebook ProfilePictureView转换为位图以在ImageView中使用,android,facebook-graph-api,android-imageview,android-bitmap,profile-picture,Android,Facebook Graph Api,Android Imageview,Android Bitmap,Profile Picture,我正在尝试将从Facebook返回的个人资料图像转换为位图,以便在需要位图的方法中使用它。我可以通过使用下面的代码获得配置文件图片,并将其放入ProfilePictureView。我还创建了另外两个用于测试的视图…CircleImageView和ImageView。到目前为止,只有ProfilePictureView显示了该图像。下面是我尝试过的代码示例,尽管我尝试过在web上找到的几种创建位图的方法,但都没有成功。如有任何建议/解决方案,将不胜感激 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();
ProfilePictureView ProfilePictureView;
profilePictureView=(profilePictureView)findViewById(R.id.facebookPictureView);
profilePictureView.setProfileId(userId)//这用于获取和设置配置文件pic
standardImageView=(ImageView)findViewById(R.id.ImageView);
circleImageView=(circleImageView)findViewById(R.id.profImage);
AsyncTask t=新的AsyncTask(){
受保护位图doInBackground(无效…p){
位图bmp=null;
试一试{
URL aURL=新URL(“http://graph.facebook.com/“+userId+”/picture?type=large”);
URLConnection conn=aURL.openConnection();
conn.SETUSECHACHES(正确);
连接();
InputStream is=conn.getInputStream();
BufferedInputStream bis=新的BufferedInputStream(is);
bmp=位图工厂.decodeStream(bis);
二、关闭();
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回bmp;
}
受保护的void onPostExecute(位图bmp){
standardImageView.setImageBitmap(bmp);
circleImageView.setImageBitmap(bmp);
}
};
t、 执行();
使用调试器并单步执行,我发现bmp为空。我尝试了一些其他的方法,同样的事情也发生了。救命啊
Uri imageUri;
String urlImage = null;
String id = null;
//此方法用于获取用户的id
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
id = object.getString("id"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,name,link,picture");
request.setParameters(params);
request.executeAsync();
//此方法用于获取用户的个人资料图片,并使用毕加索将图像放置在图像视图中。当使用此选项时,您不必获取位图对象,毕加索将直接将字符串转换为位图
urlImage = "https://graph.facebook.com/" + id + "/picture?width=" + AppController.convertDpToPx(60);
Log.d("url", urlImage);
imageUri = Uri.parse(urlImage);
if (!urlImage.isEmpty()) {
Picasso.with(getApplicationContext())
.load(urlImage)
.resize(AppController.convertDpToPx(60), 0)
.into(mTarget);
}
private Target mTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
mibProfileButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
mibProfileButton.setImageBitmap(getRoundedCornerBitmap(bitmap, bitmap.getWidth() * 17/60));
}
@Override
public void onBitmapFailed(Drawable drawable) {
AppController.getInstance().showError(R.string.error_image, R.drawable.repeating_bg_error);
}
@Override
public void onPrepareLoad(Drawable drawable) {
}
};
你也可以决定使用Glide,这是毕加索更好的选择
Bitmap theBitmap = null;
theBitmap = Glide.
with(getActivigetApplicationContext()).
load("your image url here").
asBitmap().
into(-1, -1).
get();
在bitbucket上使用此选项
您可以将其视为库尝试使用毕加索中的target。它会从url返回位图。