Not Being able to get and save profile picture of user in device by facebook-sdk-unity?

I am not sure what is the cause of this problem but here it goes

i am using facebook-sdk-unity to get user’s profile picture. instead of getting it from FB API, i am hitting a url(which is aquired from graph api request for profile picture) with WWW class object and instead of using yield return expression i am just running a loop untill i get www.isDone = true

But the problem is it is never getting out of the loop. so my code is freezing.

in unity editor it works fine. this problem only occurs on devices.

i have cross checked hitting image url in browser. i am getting an image in browser already, nothing wrong there.

here is my code :-

public GameObject cube;
void Start () 
{
	try
	{
		saveUserProfilePICFromURL("https://graph.facebook.com/{facebook-id of user}/picture");
		WWW www = new WWW ("file://"+Application.persistentDataPath + "/UserPIC.png");
		while(!www.isDone);
		cube.renderer.material.mainTexture = www.texture;
	}
	catch(System.Exception e)
	{
		Debug.Log("Exception: WebServices_Start "+e.Message);
	}	
}
internal void saveUserProfilePICFromURL(string targetURL)
{
	targetURL = targetURL.Replace(@"\/","/");
	Debug.Log ("target url "+targetURL);
	WWW www = new WWW(targetURL);
	//yield return www;
	Debug.Log ("www.isStarted ");
	int i = 0;
	while(!www.isDone)
	{
		Debug.Log ("www.isInProgress "+i);// program gets trapped in this loop
		i++;
	};
	Debug.Log ("www.isDone ");
	Texture2D PIC = www.texture;
	//cube.renderer.material.mainTexture = PIC;
	byte[] bytes = PIC.EncodeToPNG();
	Debug.Log ("PIC data length "+bytes.Length);
	string path = Application.persistentDataPath + "/UserPIC.png";
	Debug.Log ("path "+path);
	System.IO.File.WriteAllBytes(path, bytes);
}

So the problem is, program never gets out of the loop.

how ever if i use the following code for downloading image. i am getting the image but its always empty. :-

internal void saveUserProfilePICFromURL(string targetURL)
{
	string destinationURL = Application.persistentDataPath + "/UserPIC.png";
	targetURL = targetURL.Replace(@"\/","/");
	WebClient webClient = new WebClient();
	webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
	webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
	webClient.DownloadFileAsync (new Uri (targetURL), destinationURL);
}

**Your main answer is to use **

FB.API(me?fields=picture{url},HttpMethod.GET, somecallback);

you need to specify what you want like the url by picture{url}
check other params at graph api
then in somecallback

private void somecallback()
{
    IDictionary<string, object> dict = result.ResultDictionary;
    string FBProfilePicURL = ((dict["picture"] as IDictionary<string, object>)["data"] as IDictionary<string, object>)["url"].ToString();
}

sorry fot psuedo code but I used it like that

You don’t need to ckeck if its done, simply

WWW www = new WWW(FBProfilePicURL );

yield return www;

//rest code here

And note the URL returned from graph is not permanent so if you are doing like me making a leaderboards you would have to do a workaround