在Android中读取外部图像

时间:2015-07-25 16:22:17

标签: java android image

我尝试读取外部图像,但我有错误

  

“android.os.NetworkOnMainThreadException”和   “http.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)”

OnCreate

imageView = (ImageView) findViewById(R.id.image_view);
downloadFile(imageHttpAddress);

功能downloadFile

void downloadFile(String imageHttpAddress) {
    URL imageUrl = null;
    try {
        imageUrl = new URL(imageHttpAddress);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.connect();
        loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
        imageView.setImageBitmap(loadedImage);
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Error cargando la imagen: "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用此功能;)

public Bitmap DownloadImage(String STRURL) {
Bitmap bitmap = null;
InputStream in = null;       
try {
    int response = -1;
    URL url = new URL(STRURL);
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection))             
    throw new IOException("Not an HTTP connection");
    try{
    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestMethod("GET");
    httpConn.connect();
    response = httpConn.getResponseCode();  
    if (response == HttpURLConnection.HTTP_OK) {
    in = httpConn.getInputStream();
    }                    
    }catch(Exception ex) {
    throw new IOException("Error connecting"); 
    }
    bitmap = BitmapFactory.decodeStream(in);
    in.close();
    }catch (IOException e1) {
    e1.printStackTrace();
    }
    return bitmap
}

答案 1 :(得分:0)

From Android docs

您不应该在UI线程上执行网络操作。

要避免此异常,您可以使用AsyncTask来处理您的请求。

    imageView = (ImageView) findViewById(R.id.image_view);

        new AsyncTask<String, Void, Bitmap>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected Bitmap doInBackground(String... url) {
                return downloadFile(url[0]);
            }

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

        private Bitmap downloadFile(String imageHttpAddress) {
            URL imageUrl = null;
            try {
                imageUrl = new URL(imageHttpAddress);
                HttpURLConnection conn =(HttpURLConnection)imageUrl.openConnection(); 
                conn.connect();
                loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
                return loadedImage; 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

更好的是,为android使用一些异步图像处理库。互联网上有很多这样的东西。一个非常受欢迎的是picasso