我的android应用程序在从流中读取图像后停止

时间:2016-06-12 12:28:37

标签: java android bitmap

private void GetUserImage() {
    try {
        imguser=(ImageView)findViewById(R.id.imguser);
        String imageUrl= "http://10.0.2.2:8080/Clibrary/biss.jpg";
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
        imguser.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

尝试执行此代码时,我的应用程序停止工作并返回此错误:

FATAL EXCEPTION: main
Process: com.bassem.donateme, PID: 17076                                                                      java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.bassem.donateme/com.bassem.donateme.profile}: android.os.NetworkOnMainThreadException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: android.os.NetworkOnMainThreadException
   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
   at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:249)
   at libcore.io.IoBridge.recvfrom(IoBridge.java:549)
   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:481)
   at java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
   at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
   at com.android.okhttp.okio.Okio$2.read(Okio.java:135)
   at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
   at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
   at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
   at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
   at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
   at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
   at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
   at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaders(HttpURLConnectionImpl.java:150)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:194)
   at java.net.URLConnection.getContentType(URLConnection.java:325)
   at java.net.URLConnection.getContent(URLConnection.java:193)
   at java.net.URL.getContent(URL.java:455)
   at com.bassem.donateme.profile.GetUserImage(profile.java:62)
   at com.bassem.donateme.profile.onCreate(profile.java:39)
   at android.app.Activity.performCreate(Activity.java:6237)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method)

1 个答案:

答案 0 :(得分:0)

这是因为您正在尝试在主线程上进行网络操作。要解决此问题,请使用AsyncTask加载图像。考虑在后台线程上进行所有繁重的操作。

 private void GetUserImage() {

            imguser=(ImageView)findViewById(R.id.imguser);
            new AsyncTask<Void, Void, Bitmap>() {

                @Override
                protected Bitmap doInBackground(Void... params) {
                    Bitmap bitmap = null;
                    try {
                        String imageUrl = "http://10.0.2.2:8080/Clibrary/biss.jpg";
                      bitmap=  BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } 
                    return bitmap;
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    super.onPostExecute(bitmap);
                    imguser.setImageBitmap(bitmap);
                }
            }.execute();

        }