我正在尝试使用以下代码获取facebook个人资料图片。我尝试在日志中的链接看起来很好,但在getContent()部分我得到错误。关于为什么会发生这种情况的任何想法?
我的代码是:
imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
System.out.println("problem 7");
System.out.println(imgUrl);
InputStream in = (InputStream) imgUrl.getContent();
System.out.println("problem 7.5");
Bitmap bitmap = BitmapFactory.decodeStream(in);
System.out.println("problem 8");
错误如下:
04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: problem 7
04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
04-21 15:59:58.481 18711-18711/com.my_last.mylast D/AndroidRuntime: Shutting down VM
04-21 15:59:58.481 18711-18711/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
04-21 15:59:58.481 18711-18711/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at java.net.URLConnection.getContent(URLConnection.java:194)
at libcore.net.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:174)
at java.net.URL.getContent(URL.java:447)
at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:365)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
04-21 15:59:58.992 18711-18713/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3
编辑:
我更新了我的代码,现在收到如下错误:
Thread thread = new Thread() {
public void run() {
Message message = new Message();
try {
imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
System.out.println("problem 7");
System.out.println(imgUrl);
Bitmap bitmap = BitmapFactory.decodeStream(imgUrl.openConnection().getInputStream());
System.out.println("problem 8");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
image2 = new ParseFile("profilePhoto2.jpg", byteArray1, "image/jpg");
} catch (Exception e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
}
};
thread.start();
错误讯息:
04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: problem 7
04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
04-21 16:40:29.118 10885-10885/com.my_last.mylast D/AndroidRuntime: Shutting down VM
04-21 16:40:29.118 10885-10885/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
04-21 16:40:29.118 10885-10885/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: value may not be null.
at com.parse.ParseObject.performPut(ParseObject.java:2860)
at com.parse.ParseObject.put(ParseObject.java:2851)
at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:406)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
04-21 16:40:29.648 10885-10892/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3
04-21 16:40:29.658 10885-10892/com.my_last.mylast I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
04-21 16:40:29.777 10885-12039/com.my_last.mylast I/System.out: problem 8
答案 0 :(得分:1)
您可以使用Thread或AsyncTask在后台线程中下载图像。以下是使用thread进行此操作的示例。
Thread thread = new Thread() {
public void run() {
try{
imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
System.out.println("problem 7");
System.out.println(imgUrl);
InputStream in = (InputStream) imgUrl.getContent();
System.out.println("problem 7.5");
Bitmap bitmap = BitmapFactory.decodeStream(in);
System.out.println("problem 8");
} catch (Exception e) {
}
}
};
thread.start();
}