当前位置:操作系统 > 安卓/Android >>

Android中显示网络上的图片

在android中显示网络上的图片,需要先根据url找到图片地址,然后把该图片转化成java的InputStream,然后把该InputStream流转化成BitMap,BitMap可以直接显示在android中的ImageView里。这就是显示网络上图片的思路,实现起来很简单。下面让我们看一下实现起来的过程。

首先在AndroidManifest.xml中给程序加易做图问Internet的权限:

<uses-permissionandroid:name="android.permission.INTERNET" /> 

 

         然后在布局文件中加入一个ImageView,用来显示网络上的图片:

 


[html] <span style="font-size:16px;"><?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" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
    <ImageView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/imageView" /> 
 
</LinearLayout> 
</span> 
<span style="font-size:16px;"><?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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" />

</LinearLayout>
</span>

 


在主程序的Activity中写从网络中得到图片,并转化成InputStream,然后再转化成可以显示在ImageView里的Bitmap。

 

[java] <span style="font-size:16px;">package com.image; 
 
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 NetImageActivity extends Activity { 
    /** Called when the activity is first created. */ 
     String imageUrl = "/2012/0416/20120416110127604.jpg";   
     Bitmap bmImg;   
     ImageView imView;   
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        imView = (ImageView) findViewById(R.id.imageView);   
        imView.setImageBitmap(returnBitMap(imageUrl));   
    } 
     
    public 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;   
    }   

</span> 
<span style="font-size:16px;">package com.image;

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 NetImageActivity extends Activity {
    /** Called when the activity is first created. */
  String imageUrl = "/2012/0416/20120416110127604.jpg"; 
  Bitmap bmImg; 
  ImageView imView; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imView = (ImageView) findViewById(R.id.imageView); 
        imView.setImageBitmap(returnBitMap(imageUrl)); 
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,