简体   繁体   English

在Android中从网址获取图片

[英]Fetching image from url in Android

I am trying to Fetch image from Url, but i'm getting I/O exception. 我正在尝试从网址获取图片,但是我遇到了I / O异常。 I really have no idea why. 我真的不知道为什么。

 try {
        URL profilePicUrl =  new URL("http://houssup.netau.net/vivek_thumbnail.jpg");

        HttpURLConnection connection =(HttpURLConnection) profilePicUrl.openConnection();

        connection.setDoInput(true);


        connection.connect();

        InputStream inputStreamImage = connection.getInputStream();

        Bitmap profileImage = BitmapFactory.decodeStream(inputStreamImage);

        holder.profilePic.setImageBitmap(profileImage);


    } catch (MalformedURLException e) {
        Log.e("Profile Image","Error in URL");
         e.printStackTrace();
    } catch (IOException e) {
        Log.e("Profile Image","Error in IO");
        e.printStackTrace();
    }

Also i want to make point that, HttpURLconnection is throwing exception everywhere in my app. 我也想指出一点,HttpURLconnection在我的应用程序中到处都引发异常。 I mean in every class, I am not able to use this class. 我的意思是在每个班级中,我都无法使用该班级。

I tried that code and the error is this: 我尝试了该代码,错误是这样的:

Caused by: android.os.NetworkOnMainThreadException 造成原因:android.os.NetworkOnMainThreadException

so the solution is to include this on your code: 所以解决方案是将此包含在您的代码中:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
StrictMode.setThreadPolicy(policy);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM