简体   繁体   English

无法使用图形API Facebook Facebook SDK 4.0获取配置文件图片

[英]Unable to fetch profile pic using graph api Facebook sdk 4.0

I am unable to fetch image using 我无法使用获取图像

 Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/100001220051873/picture");
            if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
            else
                Toast.makeText(getActivity(),"null it is",Toast.LENGTH_SHORT).show();

Where getBitmapFromURL is 其中getBitmapFromURL是

public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

But when I use below everything works fine 但是当我在下面使用时,一切正常

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

But I have read there Strict mode in android 2.2 that this policy is not safe to use and not just this, it is also blocking my main UI for few seconds. 但是我已经在android 2.2中读到了严格模式 ,该策略不仅安全而且使用不安全,还会阻塞我的主界面几秒钟。

I want to know first, why I am unable to fetch facebook profile pic without using above policy and Is there any way that I can avoid above policy and may able to fetch my profile pic? 我想先知道,为什么不使用上述政策就无法获取Facebook个人资料图片,有什么办法可以避免上述政策,并且可以获取我的个人资料图片? Thanks in advance :) 提前致谢 :)

Following is the working code: 以下是工作代码:

GraphRequest request = GraphRequest.newMeRequest(
            accessToken,
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    // Application code
                    Log.d("response","response"+object.toString());
                    Intent intent=new Intent(MainActivity.this, ProfilePictureActivity.class);
                    try {
                        intent.putExtra("pic_url",object.getJSONObject("picture").getJSONObject("data").getString("url"));

                        startActivity(intent);
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,gender,birthday,picture.width(300)");
    request.setParameters(parameters);
    request.executeAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM