android > ImageView 加载本地/服务器图片

先是布局 . xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
	

    
</LinearLayout>

 

然后是一个 加载 本地 / 服务器  图片 的 class 里面有2 个方法 一个加载本地 的 一个加载 服务器 的 参数有URL

 

 

package b22.com;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapUtils {
	  /** 
	    * 加载本地图片 
	    * @param url 
	    * @return 
	    */  
	    public static Bitmap getLoacalBitmapByAssets(Context c, String url)  
	    {  
	        Bitmap bitmap = null;  
	        InputStream in = null;  
	        try  
	        {  
	            in = c.getResources().getAssets().open(url);  
	            bitmap = BitmapFactory.decodeStream(in);  
	        }  
	        catch (IOException e)  
	        {  
	            e.printStackTrace();  
	        }  
	        finally  
	        {  
	            closeStream(in, null);  
	        }  
	        return bitmap;  
	    }  
	  
	    /** 
	    * 从服务器取图片 
	    * @param url 
	    * @return 
	    */  
	    public static Bitmap getHttpBitmap(String url)  
	    {  
	        URL myFileUrl = null;  
	        Bitmap bitmap = null;  
	        InputStream in = null;  
	        try  
	        {  
	            myFileUrl = new URL(url);  
	            HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();  
	            conn.setConnectTimeout(0);  
	            conn.setDoInput(true);  
	            conn.connect();  
	            in = conn.getInputStream();  
	            bitmap = BitmapFactory.decodeStream(in);  
	        }  
	        catch (IOException e)  
	        {  
	            e.printStackTrace();  
	        }  
	        finally  
	        {  
	            closeStream(in, null);  
	        }  
	        return bitmap;  
	    }  
	  
	    /**  
	     * 关闭输入输出流 
	     * @param in 
	     * @param out 
	     */  
	    public static void closeStream(InputStream in, OutputStream out)  
	    {  
	        try  
	        {  
	            if (null != in)  
	            {  
	                in.close();  
	            }  
	            if (null != out)  
	            {  
	                out.close();  
	            }  
	        }  
	        catch (IOException e)  
	        {  
	            e.printStackTrace();  
	        }  
	    }  
}

 

然后是 主 activity 调用 加载图片的 class 方法 然后 里面的图片根路径是 assets 文件夹 

 

 

package b22.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;

public class B22Activity extends Activity {
    /** Called when the activity is first created. */
	
	private ImageView imageLayout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageLayout = (ImageView)findViewById(R.id.image);
        
        String imagePath = "rdt_right.png";        
        imageLayout.setImageBitmap(BitmapUtils.getLoacalBitmapByAssets(B22Activity.this, imagePath));
        
        
    }
    
    
}
 

 

你可能感兴趣的:(imageview)