android - 将 Facebook ProfilePictureView 转换为位图以在 ImageView 中使用

标签 android facebook-graph-api android-imageview android-bitmap profile-picture

我正在尝试将从 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/

相关文章:

android - 使用 RxJava 等待完成多个任务

android - Android Market 团队已禁用此评级选项

android - 恢复状态时预期适配器为 'fresh'

Android 通用用户代理 (UA)

php - 如何缓存 PHP 生成的动态页面并包含开放图形元信息

spring - Facebook OAuth 不返回用户的电子邮件

Facebook 图形 API : Update Page Post Message

android - 动态更改按钮上的 Imageview

android - 如何在android中将图像从一个 Activity 发送到另一个 Activity ?

android - 如何在位图或绘图中添加文本?