Question

Previously I was getting Facebook user profile picture like this and everything worked fine till recently.

But now every time it returns null.

Here is the url:

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

Could this happen because of the redirect from facebook? Because if you open this URL in browser you get redirected to something like this: https://fbcdn-profile-a.akamaihd.net/....... Is there a way to fix this?

Here is my 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;
}

}

I am getting this picture from facebook, making it rounded and then displaying it. But it used to work fine two weeks ago! If I pass the redirected url which looks something like this: https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/t1.0-1/c12.12.155.155/s100x100/....jpg Then it shows the image!

Here is the error:

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
Was it helpful?

Solution

I'm not sure, but you can try with the actual url (the one to which fb redirects).

To get this url use redirect=0, like this-

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

Then you'll get the response as-

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

Fetch this url from the dictionary and then try. I'm not sure but it helps.

But if it doesn't work, then it has to do with your code, I would suggest you to print the exact error/exception.

Edit:

I found that:

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

So, try to load images from https instead of http : "https://graph.facebook.com/USER_ID/picture"; since image's url is "https://fbcdn-profile-a.akamaihd.net/...."

Then BitmapFactory.decodeStream shall work again.

OTHER TIPS

Original URL's protocol is HTTP where as the redirected URL's protocol is HTTPS.

Automatic redirection happens only if the original and redirected URL's follow the same protocol.

so change your Image url From "http://graph.facebook.com/"+userID+ .. to "https://graph.facebook.com/"+userID+ ..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top