图像URL到位图

时间:2015-03-25 06:02:41

标签: android facebook bitmap bitmapimage bitmapfactory

我从Facebook SDK获取个人资料图片,它返回url。当我在浏览器中打开它时,它会显示图像。但是当我将其转换为位图时,它返回null。这是我的代码:

String id = user.getId();
try {
      URL image_path = new URL("http://graph.facebook.com/"+ id+ "/picture?type=large");
      Bitmap profPict = BitmapFactory.decodeStream(image_path.openConnection().getInputStream());
      System.out.println("profPict::> " + profPict);
      ImageView img= (ImageView)findViewById(R.id.textView2);
      img.setImageBitmap(profPict);
      System.out.println("image::> " + image_path);
}
catch (Exception e) {
      e.printStackTrace();
} 

2 个答案:

答案 0 :(得分:0)

 public  Bitmap getBitmapFromURL(String srcurl){
        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;
        }
    }

答案 1 :(得分:0)

使用任何可接受URL和ImageView的第三方库,并在ImageView中下载和设置图像。

Android Universal Image Loader非常有用。