2

我正在尝试转换从 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 为空。我尝试了其他一些方法,并且发生了同样的事情。帮助!

4

3 回答 3

2

您也可以决定使用 Glide,这是 Picasso 的更好选择

Bitmap theBitmap = null;
theBitmap = Glide.
      with(getActivigetApplicationContext()).
      load("your image url here").
      asBitmap().
      into(-1, -1).
      get();
于 2015-09-30T04:01:12.593 回答
0

在 bitbucket 上使用此代码段

你可以把它当作图书馆

于 2015-09-30T04:25:37.090 回答
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 将图像放置在图像视图中。使用它时您不必获取位图对象,毕加索将直接将字符串转换为位图

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) {

        }
    };
于 2015-09-30T03:57:59.990 回答