之前我正在获得这样的Facebook用户个人资料图片,直到最近一切正常。
但现在每次都返回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上获取这张照片,将其四舍五入然后显示出来。但它曾经在两周前工作正常!如果我传递的重定向网址如下所示: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
答案 0 :(得分:10)
我不确定,但您可以尝试使用实际网址(fb重定向的网址)。
要获取此网址,请使用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
}
}
从字典中获取此网址然后尝试。我不确定,但有帮助。
但如果它不起作用,那么它与您的代码有关,我建议您打印确切的错误/异常。
修改强>
我发现:
当原始协议和重定向协议相同时,自动重定向会自动生效。
因此,请尝试从 https 而非 http 加载图片:&#34; https://graph.facebook.com/USER_ID/picture&#34 ;;因为图片的网址是&#34; https://fbcdn-profile-a.akamaihd.net/ ....&#34;
然后BitmapFactory.decodeStream
将再次运作。
答案 1 :(得分:4)
原始网址的协议是HTTP,其中重定向的网址协议是HTTPS。
仅当原始和重定向的URL遵循相同的协议时才会发生自动重定向。
因此请更改您的图片网址 从 &#34; http://graph.facebook.com/&#34; + userID + .. 至 &#34; https://graph.facebook.com/&#34; + userID + ..