Android : Getting a Bitmap from a url connection (graph.facebook.com) that redirects to another url

前端 未结 2 1975
情书的邮戳
情书的邮戳 2021-01-15 15:59

I have done everything to get a url where i can get the profile pic of a facebook user.

The only problem left now is to get that image into a bitmap object.

2条回答
  •  轮回少年
    2021-01-15 16:30

    Update : The method given by Sahil Mittal works absolutely fine and i would definitely ask you guys to use his method.

    As for the method I used in the meantime you can read this answer.

    I dont know whether the method given by Sahil Mittal works or not.

    I havent tried that but i used another code segment which seems to work for me.

    But i will get back whether it works or not as soon as i try it.

    Bitmap getUserBitmap(String username){
    
    
            HttpGet httpRequest = null;
    
            Bitmap userbmp = null;
    
            try {
                URL url = new URL("http://graph.facebook.com/" + username + "/picture?type=small");
    
                httpRequest = new HttpGet(url.toURI());
    
                HttpClient httpclient = new DefaultHttpClient();
    
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    
                HttpEntity entity = response.getEntity();
    
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    
                InputStream instream = bufHttpEntity.getContent();
    
                userbmp = BitmapFactory.decodeStream(instream);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            userbmp = getRoundedShape(userbmp); // function call to make the image round
            return (userbmp);
        }
    

提交回复
热议问题