将 Facebook ProfilePictureView 转换为位图以在 ImageView 中使用
Converting Facebook ProfilePictureView to bitmap to use in ImageView
我正在尝试转换从 Facebook 返回的个人资料图像并将其更改为位图,以便我可以在需要位图的方法中使用它。我可以使用下面的代码获取个人资料图片并将其放入 ProfilePictureView。我还创建了另外两个用于测试的视图...一个 CircleImageView 和一个 ImageView。到目前为止,只有 ProfilePictureView 显示图像。这是我尝试过的代码示例,尽管我已经尝试了在网上找到的几种创建位图的方法并且 none 已经奏效。任何 suggestions/solutions 将不胜感激!
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 为空。我尝试了其他几种方法,同样的事情也发生了。帮助!
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();
// 此方法用于获取用户的个人资料图片并使用 Picasso 将图像放置在图像视图中。使用它时你不必获取位图对象,Picasso 会直接将字符串转换为位图
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,这是 Picasso 更好的选择
Bitmap theBitmap = null;
theBitmap = Glide.
with(getActivigetApplicationContext()).
load("your image url here").
asBitmap().
into(-1, -1).
get();
在 bitbucket
上使用这个 Snippet
你可以像图书馆一样对待它
我正在尝试转换从 Facebook 返回的个人资料图像并将其更改为位图,以便我可以在需要位图的方法中使用它。我可以使用下面的代码获取个人资料图片并将其放入 ProfilePictureView。我还创建了另外两个用于测试的视图...一个 CircleImageView 和一个 ImageView。到目前为止,只有 ProfilePictureView 显示图像。这是我尝试过的代码示例,尽管我已经尝试了在网上找到的几种创建位图的方法并且 none 已经奏效。任何 suggestions/solutions 将不胜感激!
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 为空。我尝试了其他几种方法,同样的事情也发生了。帮助!
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();
// 此方法用于获取用户的个人资料图片并使用 Picasso 将图像放置在图像视图中。使用它时你不必获取位图对象,Picasso 会直接将字符串转换为位图
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,这是 Picasso 更好的选择
Bitmap theBitmap = null;
theBitmap = Glide.
with(getActivigetApplicationContext()).
load("your image url here").
asBitmap().
into(-1, -1).
get();
在 bitbucket
上使用这个 Snippet你可以像图书馆一样对待它