• Android网络图片显示在ImageView 上面


    在写这篇博文的时候,我參与了一个项目的开发,里面涉及了非常多网络调用相关的问题,我记得我在刚刚開始做android项目的时候,以前就遇到这个问题,当时在网上搜索了一下,发现了一篇博文,如今与大家分享一下,http://www.open-open.com/lib/view/open1376128628881.html

    事实上这篇文章的思想是有问题的,由于网络是需要不断的轮询訪问的,所以必需要放在线程中,而不应该直接放在onCreate方法中,我对这段程序进行了一定的改动,将它们放在线程中更新就能够了。

    这个事MainActivity.java的代码:

    package com.test.picture;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    	private ImageView imageView;
    	private String picturePath = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		initView();
    		new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				final Bitmap bitmap = returnBitMap(picturePath);
    				imageView.post(new Runnable() {
    
    					@Override
    					public void run() {
    						// TODO Auto-generated method stub
    						imageView.setImageBitmap(bitmap);
    					}
    				});
    			}
    		}).start();
    	}
    
    	private Bitmap returnBitMap(String url) {
    		URL myFileUrl = null;
    		Bitmap bitmap = null;
    		try {
    			myFileUrl = new URL(url);
    		} catch (MalformedURLException e) {
    			e.printStackTrace();
    		}
    		try {
    			HttpURLConnection conn = (HttpURLConnection) myFileUrl
    					.openConnection();
    			conn.setDoInput(true);
    			conn.connect();
    			InputStream is = conn.getInputStream();
    			bitmap = BitmapFactory.decodeStream(is);
    			is.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return bitmap;
    	}
    
    	private void initView() {
    		imageView = (ImageView) findViewById(R.id.picture);
    	}
    }
    

    这是布局文件的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/display_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

    当然我们也要在AndroidManifest.xml中加入�对应的网络权限。


    我们在MainActivity.java中的onCreate方法中创建了一个新的线程,可是我们也明确不能够在非UI线程中操作用户界面,那么我们不能够直接在新的线程中操作UI,所以我们还要将改变post到UI线程中去。

    上面的方法执行时没有问题的,谢谢大家。


  • 相关阅读:
    页面抛出的"Unable to validate data"错误
    oracle的sqlnet.ora,tnsnames.ora,listener.ora三个配置文件 water
    Win7下完全卸载Oracle 11g water
    抛出多个异常 water
    【转】有关Oracle随机字符串的生成方法及具体应用 water
    【转】oracle 中随机取一条记录的两种方法 water
    【转】Oracle索引列NULL值引发执行计划该表的测试示例 water
    Oracle 取随机数 water
    dbms_output.put_line长度限制问题 water
    oracle 监听启动、停止、查看命令 water
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4077073.html
Copyright © 2020-2023  润新知