gpt4 book ai didi

java - 发送图像文件时如何修复 NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-12-01 10:16:31 27 4
gpt4 key购买 nike

实际上,我尝试将图像文件从 Android 发送到另一台计算机,但出现错误:android.os.NetworkOnMainThreadException

03-08 00:57:40.474: E/AndroidRuntime(1201): FATAL EXCEPTION: main
03-08 00:57:40.474: E/AndroidRuntime(1201): Process: com.example.singh, PID: 1201
03-08 00:57:40.474: E/AndroidRuntime(1201): android.os.NetworkOnMainThreadException
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
03-08 00:57:40.474: E/AndroidRuntime(1201): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
03-08 00:57:40.474: E/AndroidRuntime(1201): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-08 00:57:40.474: E/AndroidRuntime(1201): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.Socket.startupSocket(Socket.java:567)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.Socket.tryAllAddresses(Socket.java:128)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.Socket.<init>(Socket.java:178)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.net.Socket.<init>(Socket.java:150)
03-08 00:57:40.474: E/AndroidRuntime(1201): at com.example.singh.MainActivity$2.onClick(MainActivity.java:65)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.view.View.performClick(View.java:4438)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.view.View$PerformClick.run(View.java:18422)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.os.Handler.handleCallback(Handler.java:733)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.os.Looper.loop(Looper.java:136)
03-08 00:57:40.474: E/AndroidRuntime(1201): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:57:40.474: E/AndroidRuntime(1201): at java.lang.reflect.Method.invoke(Method.java:515)
03-08 00:57:40.474: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-08 00:57:40.474: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-08 00:57:40.474: E/AndroidRuntime(1201): at dalvik.system.NativeStart.main(Native Method)
03-08 00:57:42.944: I/Process(1201): Sending signal. PID: 1201 SIG: 9

最佳答案

您正在尝试在主线程上发送/上传图像。您应该在后台线程中执行此操作。您可以使用AsyncTask为了这个目的。

Android 文档:

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.

关于java - 发送图像文件时如何修复 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35860303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com