栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android从网络中获得一张图片并显示在屏幕上的实例详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android从网络中获得一张图片并显示在屏幕上的实例详解

Android从网络中获得一张图片并显示在屏幕上的实例详解

看下实现效果图:

1:androidmanifest.xml的内容

 
 
   
     
       
  
  
       
     
 
   
   
   
 
  

注意访问网络中的数据需要添加android.permission.INTERNET权限

2:MainActivity的内容

package cn.capinftotech.image; 
 
import java.io.IOException; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 
 
import com.capinfotech.service.ImageService; 
 
public class MainActivity extends Activity { 
  private static final String TAG = "MainActivity"; 
   
  private EditText urlPath = null; 
  private Button button = null; 
  private ImageView imageView = null; 
   
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    urlPath = (EditText)findViewById(R.id.urlpath); 
    button = (Button)findViewById(R.id.button); 
    imageView = (ImageView)findViewById(R.id.imageView); 
     
    button.setonClickListener(new View.onClickListener() { 

      @Override 
      public void onClick(View v) { 
 String urlPathContent = urlPath.getText().toString(); 
 try { 
   byte[] data = ImageService.getImage(urlPathContent); 
   Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); //生成位图 
   imageView.setImageBitmap(bitmap);  //显示图片 
 } catch (IOException e) { 
   Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show(); //通知用户连接超时信息 
   Log.i(TAG, e.toString()); 
 } 
  
      } 
    }); 
  } 
} 

3:ImageService类的内容

package com.capinfotech.service; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import com.capinfotech.utils.StreamTool; 
 
public class ImageService { 
   
  public static byte[] getImage(String path) throws IOException { 
    URL url = new URL(path); 
    HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
    conn.setRequestMethod("GET");  //设置请求方法为GET 
    conn.setReadTimeout(5*1000);  //设置请求过时时间为5秒 
    InputStream inputStream = conn.getInputStream();  //通过输入流获得图片数据 
    byte[] data = StreamTool.readInputStream(inputStream);   //获得图片的二进制数据 
    return data; 
     
  } 
} 

4:StreamTool的内容

package com.capinfotech.utils; 
 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
 
public class StreamTool { 
 
   
  public static byte[] readInputStream(InputStream inputStream) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int len = 0; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    while((len = inputStream.read(buffer)) != -1) { 
      bos.write(buffer, 0, len); 
    } 
    bos.close(); 
    return bos.toByteArray(); 
     
  } 
} 

5:程序中用到的字符串资源strings.xml里的内容

 
 
  Hello World, MainActivity! 
  图片浏览器 
  网络图片地址 
  显示 
  网络连接超时 
 

6:程序布局文件main.xml的内容

 
 
 
 

以上使用Android 获取网路图片并显示的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号