我正在尝试转换从Facebook返回的配置文件图像并将其更改为位图,以便我可以在需要位图的方法中使用它。我可以使用下面的代码获取个人资料图片并将其放入ProfilePictureView。我还创建了另外两个用于测试的视图......一个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为null。我尝试了其他一些方法,但同样的事情发生了。救命!
答案 0 :(得分:2)
你也可以决定使用Glide,这是Picasso的更好选择
Bitmap theBitmap = null;
theBitmap = Glide.
with(getActivigetApplicationContext()).
load("your image url here").
asBitmap().
into(-1, -1).
get();
答案 1 :(得分:0)
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) {
}
};
答案 2 :(得分:0)
在bitbucket上使用此Snippet
您可以将其视为图书馆