从URL下载Bitmap不起作用

时间:2015-08-07 16:18:22

标签: android url android-bitmap

我有这个简单的代码:

public Bitmap getimg(String src){
try {
    Bitmap myBitmap;
    URL url = new URL(src);

    myBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    return myBitmap;
}catch (IOException e) {
    return null;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

ImageView iv =(ImageView)findViewById(R.id.imageView);         位图myBitmap; MYBITMAP = GETIMG( “http://customerfx.com/blogs/integrationblog/Standard%20Activity%20List%20view.png”);         iv.setImageBitmap(MYBITMAP); }

但它仍然给我这个错误:

ss: com.inc.nicky.bitmapurltest, PID: 19302
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inc.nicky.bitmapurltest/com.inc.nicky.bitmapurltest.MainActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
        at android.app.ActivityThread.access$900(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:390)
        at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:343)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
        at com.inc.nicky.bitmapurltest.MainActivity.getimg(MainActivity.java:22)
        at com.inc.nicky.bitmapurltest.MainActivity.onCreate(MainActivity.java:33)
        at android.app.Activity.performCreate(Activity.java:5451)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)

0 个答案:

没有答案