无法获取Facebook个人资料图片

3

我已经尝试了几乎所有提到的方法,但仍然无法在我的Android应用程序中获取已登录用户的个人资料图片。 以下是我正在使用的代码:-

new AsyncTask<String, Void, Bitmap>() {

        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                URL url = new URL(params[0]);
                return BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (Exception e) {
                Log.d("ERROR", "Unable to get Image");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(saveFlag) {
                saveImage(bitmap);
            }
            imageView.setImageBitmap(bitmap);
        }
    }.execute(url);

图片的URL为:http://graph.facebook.com/{ID}/picture。 当我在浏览器中访问该URL时,可以看到图片。 我已经尝试了http和https。

有人能帮忙吗?

编辑:我不想使用除FB或普通Android的其他API。

1个回答

0

你可以使用Picasso,并像这样使用Picasso设置图像:

URL url = new URL(params[0]);

Picasso.with(context).load(url.toString()).into(imageView);

http://square.github.io/picasso/

在异步任务中调用以下方法,并将返回的位图设置为图像。

private Bitmap getImage(String imageUrl, int desiredWidth, int desiredHeight)
    {   
           private Bitmap image = null;
           int inSampleSize = 0;


            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inJustDecodeBounds = true;

            options.inSampleSize = inSampleSize;

            try
            {
                URL url = new URL(imageUrl);

                HttpURLConnection connection = (HttpURLConnection)url.openConnection();

                InputStream stream = connection.getInputStream();

                image = BitmapFactory.decodeStream(stream, null, options);

                int imageWidth = options.outWidth;

                int imageHeight = options.outHeight;

                if(imageWidth > desiredWidth || imageHeight > desiredHeight)
                {   
                    System.out.println("imageWidth:"+imageWidth+", imageHeight:"+imageHeight);

                    inSampleSize = inSampleSize + 2;

                    getImage(imageUrl);
                }
                else
                {   
                    options.inJustDecodeBounds = false;

                    connection = (HttpURLConnection)url.openConnection();

                    stream = connection.getInputStream();

                    image = BitmapFactory.decodeStream(stream, null, options);

                    return image;
                }
            }

            catch(Exception e)
            {
                Log.e("getImage", e.toString());
            }

        return image;
    }

1
我不想使用其他api来完成这项任务。我能否只用基本的代码和Facebook api来完成? - deep
这不是一个API,而是一个库,它允许您缓存图像,使用图像创建不同的形状,允许您放置占位符等等。它使图像处理变得更加容易。 - Aakash
1
我尝试了你的代码,甚至使用了Picasso库。我能够从Google个人资料中获取图像,这是我以前就能做到的,但仍然无法通过Facebook获取图像。 - deep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接