【问题标题】:Unable to fetch facebook profile picture无法获取 Facebook 个人资料图片
【发布时间】:2015-10-01 19:25:14
【问题描述】:

我几乎尝试了任何地方提到的所有内容,但我仍然无法在我的 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);

图片的网址是:- http://graph.facebook.com/{ID}/picture。 当我从浏览器转到 url 时,我能够看到图像。 我已经尝试过http和https。

有人帮忙。

编辑:我不想使用除 fb 或普通 android 之外的任何其他 api。

【问题讨论】:

    标签: android facebook facebook-graph-api user-profile


    【解决方案1】:

    您可以像这样使用毕加索并使用毕加索设置图像:

    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;
        }
    

    【讨论】:

    • 我不想为此使用任何其他 api。我不能用基本代码和 Facebook api 来做吗?
    • 它不是 api,它是一个库,可以让您缓存图像,用图像制作不同的形状,允许您放置占位符等等,它使图像处理变得更加容易。
    • 我试过你的代码,甚至是毕加索图书馆。我能够从谷歌个人资料中获取图像,我什至可以在之前获得但仍然无法通过 facebook 获取图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多