Facebook的个人资料图片的Android,则返回null [英] Facebook profile picture Android returns null
问题描述
previously我得到的Facebook用户的个人资料图片这样,一切都正常工作,直到最近。
但现在每次返回null。
下面是网址:
http://graph.facebook.com/用户ID + +/图片HEIGHT = 100安培;类型=正常和放大器; WIDTH = 100
会发生这种事,因为从Facebook重定向的?因为如果你在浏览器中打开此URL您重定向到这样的: HTTPS://fbcdn-profile-a.akamaihd.net / ......
。
有没有办法来解决这个问题?
下面是我的AsyncTask:
公共类GetUserPicture扩展的AsyncTask<弦乐,太虚,位图> {
ImageView的profileImage; 公共GetUserPicture(ImageView的profileImage){
this.profileImage = profileImage;
} @覆盖
保护位图doInBackground(字符串的URL ...){
字符串urldisplay =网址[0];
Log.d(显示正确的URL,urldisplay);
位图位图= NULL;
尝试{
网址URL =新的URL(urldisplay);
位= BitmapFactory.de codeStream(url.openConnection()的getInputStream());
HttpURLConnection.setFollowRedirects(真);
}赶上(例外五){
e.printStackTrace();
}
返回位图;
} @覆盖
保护无效onPostExecute(位图结果){
如果(结果!= NULL){
Log.d(非空,NOT NULLL);
}
profileImage.setImageBitmap(getRoundedCornerBitmap(结果,70));
} 受保护的静态位图getRoundedCornerBitmap(位图位图,诠释像素){
位图输出= Bitmap.createBitmap(bitmap.getWidth(),位图
.getHeight(),Config.ARGB_8888); BitmapShader bitmapShader =新BitmapShader(位图,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
涂料PNT =新的油漆();
pnt.setAntiAlias(真);
pnt.setShader(bitmapShader); 帆布帆布=新的Canvas(输出); 最终诠释颜色= 0xff424242;
最终涂料粉刷=新的油漆();
最终矩形矩形=新的Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
最后RectF rectF =新RectF(RECT);
最终浮动roundPx =像素; paint.setAntiAlias(真);
canvas.drawARGB(0,0,0,0);
paint.setColor(颜色);
canvas.drawRoundRect(rectF,roundPx,roundPx,油漆); paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(位图,矩形,矩形,油漆);
返回输出;
}
}
我从Facebook获得这张照片,使得它圆,然后显示它。但它用于正常工作两个星期前!如果我通过重定向的URL看起来是这样的: <$c$c>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/t1.0-1/c12.12.155.155/s100x100/....jpg$c$c>然后,它显示的图像!
以下是错误:
12月3日至31日:05:08.482:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
12月3日至31日:05:08.482:D / AndroidRuntime(14479):关闭VM
12月3日至31日:05:08.482:W / dalvikvm(14479):主题ID = 1:螺纹未捕获的异常退出(组= 0x41675d40)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):致命异常:主要
12月3日至31日:05:08.484:E / AndroidRuntime(14479):工艺:com.hitchhiker.mobile,PID:14479
12月3日至31日:05:08.484:E / AndroidRuntime(14479):显示java.lang.NullPointerException
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在com.hitchhiker.mobile.asynctasks.GetUserPicture.getRoundedCornerBitmap(GetUserPicture.java:54)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在com.hitchhiker.mobile.asynctasks.GetUserPicture.onPostExecute(GetUserPicture.java:50)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在com.hitchhiker.mobile.asynctasks.GetUserPicture.onPostExecute(GetUserPicture.java:1)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.os.AsyncTask.finish(AsyncTask.java:632)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.os.Handler.dispatchMessage(Handler.java:102)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.os.Looper.loop(Looper.java:136)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在android.app.ActivityThread.main(ActivityThread.java:5102)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在java.lang.reflect.Method.invokeNative(本机方法)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在java.lang.reflect.Method.invoke(Method.java:515)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12月3日至31日:05:08.484:E / AndroidRuntime(14479):在dalvik.system.NativeStart.main(本机方法)
12月3日至31日:05:08.743:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
12月3日至31日:05:09.023:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
12月3日至31日:05:09.286:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
12月3日至31日:05:09.526:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
12月3日至31日:05:09.813:D / Skia的(14479):--- SkImageDe codeR ::厂返回null
我不知道,但你可以用实际URL(到FB重定向的)尝试。
要获得此网址使用重定向= 0
,这样的 -
然后你会得到的回应是 -
{
数据:{
网址:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/t5.0-1/23122_595273610_9552621_q.jpg
is_silhouette:假的
}
}
获取从字典这个URL,然后尝试。我不知道,但它帮助。
但是,如果它不工作,那么它与您的code ++做的,我建议你打印的确切的错误/异常。
编辑:
我发现:
在原始和重定向协议是相同的自动重定向自动工作。
块引用>所以,尝试从 HTTPS 而不是 HTTP 加载图像: https://graph.facebook.com/USER_ID/picture ;由于图片的URL是 https://fbcdn-profile-a.akamaihd.net/ ......
然后
BitmapFactory.de codeStream
将再次合作。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
解决方案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.这篇关于Facebook的个人资料图片的Android,则返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!