2015-05-15 48 views
2

我一直試圖用我的應用程序將我的facebooks個人資料圖片設置爲Linearlayout背景。 這裏有兩種方法我試圖做到這一點:將facebook的個人資料圖片設置爲LinearLayout背景

第一種方式:得到了來自資料圖片的URI和轉換它來繪製

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); 
     } 
} 

現在這個總是拋出異常,所以Drawable我get是blue_material之一(在catch塊中設置Drawable),所以如果有人知道爲什麼它總是拋出一個異常,我會很感激。

第二方式:轉換的ProfilePictureViewImageView然後在ImageView使用getDrawable()

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

現在,這導致有一個背景,當有人沒有個人資料圖片時,facebook會提供默認圖片。

每當我使用decodeStream異常被拋出。(如下圖所示)

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 

我沒有想訴諸詢問有關計算器這個問題,因爲我想解決它自己,但我試了這麼長時間,所以我必須得到一些幫助。

感謝的人誰的答案:)

回答

3

我解決它!

我用ImageLoader的,以及由於某種原因,網址工作吧,這裏是代碼:

 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); 

setScaleType所有我LinearLayout延伸我的ImageView的方法。 有關通用圖像加載程序的更多信息,請訪問以下鏈接: https://github.com/nostra13/Android-Universal-Image-Loader

1

我不太清楚,但是這是我用來做

img_url =new URL("https://graph.facebook.com/"+uname+"/picture?type=normal"); 
    bmp =BitmapFactory.decodeStream(img_url.openConnection().getInputStream());//bmp a bitmap 
    linearlayout.setImageBitmap(bmp);//your linear layout 
+0

由於某種原因在我的URL上調用decodeStream方法時會引發異常 –

相關問題