2014-03-31 138 views
1

以前我得到Facebook這樣的用戶個人資料圖片,一切都很好,直到最近。Facebook的個人資料圖片Android返回null

但現在每次都返回null。

這裏是網址:

"http://graph.facebook.com/"+userID+"/picture?height=100&type=normal&width=100" 

會發生這種事,因爲從Facebook重定向的?因爲如果你在瀏覽器中打開這個URL,你會被重定向到這樣的:https://fbcdn-profile-a.akamaihd.net/......。 有沒有辦法解決這個問題?

這裏是我的AsyncTask:

public class GetUserPicture extends AsyncTask<String, Void, Bitmap> { 
    ImageView profileImage; 

    public GetUserPicture(ImageView profileImage) { 
    this.profileImage = profileImage; 
    } 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
    String urldisplay = urls[0]; 
    Log.d("SHOW CORRECT URL", urldisplay); 
     Bitmap bitmap = null; 
    try { 
     URL url = new URL(urldisplay); 
     bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
     HttpURLConnection.setFollowRedirects(true); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return bitmap; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
    if (result != null) { 
     Log.d("Not null", "NOT NULLL"); 
    } 
    profileImage.setImageBitmap(getRoundedCornerBitmap(result, 70)); 
    } 

    protected static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
      .getHeight(), Config.ARGB_8888); 

     BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
    Paint pnt = new Paint(); 
    pnt.setAntiAlias(true); 
    pnt.setShader(bitmapShader); 

    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = pixels; 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 


    return output; 
} 

}

我得到這個畫面來自Facebook,使其圓,然後顯示它。但它過去兩週前工作正常!如果我通過重定向的url,看起來像這樣:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/t1.0-1/c12.12.155.155/s100x100/....jpg然後它顯示圖像!

以下是錯誤:

03-31 12:05:08.482: D/skia(14479): --- SkImageDecoder::Factory returned null 
03-31 12:05:08.482: D/AndroidRuntime(14479): Shutting down VM 
03-31 12:05:08.482: W/dalvikvm(14479): threadid=1: thread exiting with uncaught exception (group=0x41675d40) 
03-31 12:05:08.484: E/AndroidRuntime(14479): FATAL EXCEPTION: main 
03-31 12:05:08.484: E/AndroidRuntime(14479): Process: com.hitchhiker.mobile, PID: 14479 
03-31 12:05:08.484: E/AndroidRuntime(14479): java.lang.NullPointerException 
03-31 12:05:08.484: E/AndroidRuntime(14479): at com.hitchhiker.mobile.asynctasks.GetUserPicture.getRoundedCornerBitmap(GetUserPicture.java:54) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at com.hitchhiker.mobile.asynctasks.GetUserPicture.onPostExecute(GetUserPicture.java:50) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at com.hitchhiker.mobile.asynctasks.GetUserPicture.onPostExecute(GetUserPicture.java:1) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.os.AsyncTask.finish(AsyncTask.java:632) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.os.Handler.dispatchMessage(Handler.java:102) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.os.Looper.loop(Looper.java:136) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at android.app.ActivityThread.main(ActivityThread.java:5102) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at java.lang.reflect.Method.invokeNative(Native Method) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at java.lang.reflect.Method.invoke(Method.java:515) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
03-31 12:05:08.484: E/AndroidRuntime(14479): at dalvik.system.NativeStart.main(Native Method) 
03-31 12:05:08.743: D/skia(14479): --- SkImageDecoder::Factory returned null 
03-31 12:05:09.023: D/skia(14479): --- SkImageDecoder::Factory returned null 
03-31 12:05:09.286: D/skia(14479): --- SkImageDecoder::Factory returned null 
03-31 12:05:09.526: D/skia(14479): --- SkImageDecoder::Factory returned null 
03-31 12:05:09.813: D/skia(14479): --- SkImageDecoder::Factory returned null 

回答

9

我不知道,但你可以用實際的URL(一到FB重定向)嘗試。

爲了得到這個URL中使用redirect=0,像這個 -

"http://graph.facebook.com/"+userID+"/picture?height=100&type=normal&width=100&redirect=0" 

然後你會得到原樣

{ 
    data: { 
     url: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/t5.0-1/23122_595273610_9552621_q.jpg", 
     is_silhouette: false 
    } 
} 

響應從字典中抓取該網址,然後嘗試。我不確定,但它有幫助。

但是,如果它不起作用,那麼它與你的代碼有關,我建議你打印確切的錯誤/異常。

編輯:

我發現:

Auto redirection works automatically when original and redirected protocols are same.

所以,儘量從HTTPS代替HTTP加載圖像: 「https://graph.facebook.com/USER_ID/picture」;因爲圖片的網址是「https://fbcdn-profile-a.akamaihd.net/ ....」

然後BitmapFactory.decodeStream將再次工作。

+0

我在想這個解決方案,但我會提出2個請求!我只是不明白爲什麼這個'http://graph.facebook.com/「+ userID +」/ picture?height = 100&type = normal&width = 100'不起作用,但是這個'https:// fbcdn-profile-a .akamaihd.net/hprofile-ak-prn1/t5.0-1/23122_595273610_9552621_q.jpg'顯示圖片! – Karlis

+0

你沒有得到任何錯誤/異常? –

+0

我添加了異常 – Karlis

4

原始URL的協議是HTTP,其中重定向的URL的協議是HTTPS。

只有原始URL和重定向URL遵循相同的協議纔會發生自動重定向。

因此改變你的圖片網址 從 「http://graph.facebook.com/ 」+用戶ID + .. 到 「 https://graph.facebook.com/」 +用戶ID + ..

相關問題