androidfacebookbackgrounduriprofile-picture

Setting facebook's profile picture as a LinearLayout background


I have been trying with no avail to set my facebooks profile picture as a Linearlayout background in my application. here are the two ways I tried to do this:

first way: got the uri from the profile picture and converted it to drawable

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    LinearLayout banner = (LinearLayout)findViewById(R.id.banner);
       Profile profile = Profile.getCurrentProfile();
        if(profile != null){
           // profile_pic_id.setProfileId((profile.getId()));
            Drawable d;
            Uri uri = profile.getLinkUri();
            //also tried profile.getProfilePictureUri(random_num,random_num)
            try {

                InputStream inputStream = getContentResolver().openInputStream(uri);
                 d = Drawable.createFromStream(inputStream, uri.toString());
            } catch (FileNotFoundException e) {
                d = getResources().getDrawable(R.drawable.blue_material);
            }
            banner.setBackgroundDrawable(d);
        }
}

now this always threw an exception so the Drawable i get is the blue_material one (the Drawable set in the catch block), so if anyone knows why its throwing an exception all the time I'd be grateful.

second way: converted the ProfilePictureView to ImageView and then used getDrawable() on the ImageView.

 ProfilePictureView profilePictureFB=(ProfilePictureView)findViewById(R.id.pic);
 profilePictureFB.setProfileId((profile.getId()));
 ImageView fbImage = ( ( ImageView)profilePictureFB.getChildAt( 0));
 banner.setBackgroundDrawable(fbImage.getDrawable());

now this resulted in having a background with the default picture facebook puts when someone has no profile pic.

AND whenever I use decodeStream an exception gets thrown.(example below)

URL img_url =new URL("https://graph.facebook.com/"+profile.getId()+"/picture?type=normal");
                Bitmap bmp =BitmapFactory.decodeStream(img_url.openConnection().getInputStream());//bmp a bitmap

I didnt want to resort to asking this question on stackoverflow because I wanted to solve it myself, but I tried for so very long, so I had to get some help.

Thanks for anyone who answers :)


Solution

  • I solved it!!

    i used imageLoader, and for some reason the url works with it, here is the code:

            imageView = (ImageView) findViewById(R.id.pic);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageLoader = ImageLoader.getInstance();
    
            Profile profile = Profile.getCurrentProfile();
            if(profile != null){           
                DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
                imageLoader.displayImage("http://graph.facebook.com/" + profile.getId() + "/picture?width=400&height=400", imageView, options);
               // banner.setBackgroundDrawable(imageView.getDrawable());
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    

    the method setScaleType stretches my imageview across all myLinearLayout. for more information about the universal image loader visit this link: https://github.com/nostra13/Android-Universal-Image-Loader