<强> Android从网络中获得一张图片并显示在屏幕上的实例详解强>
看下实现效果图:
1: androidmanifest.xml的内容
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;清单xmlns: android=" http://schemas.android.com/apk/res/android " 包=" cn.capinftotech.image " android: versionCode=" 1 " android: versionName=" 1.0 "比;=& lt;应用android:图标“@drawable/图标”android:标签=癅string/app_name”比; & lt;活动android: name=" .MainActivity " android: label=" @string/app_name”比; & lt; intent-filter> & lt;行动android: name=" android.intent.action.MAIN "/比; & lt;类别android: name=" android.intent.category.LAUNCHER "/比; & lt;/intent-filter> & lt;/activity> & lt;/application> & lt; uses-sdk android: minSdkVersion=" 8 "/比; & lt; uses-permission android: name=" android.permission。互联网”/比; & lt;/manifest> >之前注意访问网络中的数据需要添加android.permission。互联网权限
2: MainActivity的内容
包cn.capinftotech.image; 进口java.io.IOException; 进口android.app.Activity; 进口android.graphics.Bitmap; 进口android.graphics.BitmapFactory; 进口android.os.Bundle; 进口android.util.Log; 进口android.view.View; 进口android.widget.Button; 进口android.widget.EditText; 进口android.widget.ImageView; 进口android.widget.Toast; 进口com.capinfotech.service.ImageService; 公开课MainActivity延伸活动{ 私有静态最终字符串标签=癕ainActivity”; 私人EditText urlPath=零; 私人按钮按钮=零; 私人ImageView ImageView=零; @Override 公共空间> 包com.capinfotech.service; 进口java.io.IOException; 进口java.io.InputStream; 进口java.net.HttpURLConnection; 进口java.net.URL; 进口com.capinfotech.utils.StreamTool; 公开课ImageService { 公共静态byte [] getImage (String路径)抛出IOException { 网址URL=new URL(路径); HttpURLConnection康涅狄格州=(HttpURLConnection) url.openConnection (); conn.setRequestMethod(“获得”);//设置请求方法为 conn.setReadTimeout (5 * 1000);//设置请求过时时间为5秒 InputStream InputStream=conn.getInputStream ();//通过输入流获得图片数据 byte [] data=https://www.yisu.com/zixun/StreamTool.readInputStream (inputStream);//获得图片的二进制数据 返回数据; } } >之前4: StreamTool的内容
包com.capinfotech.utils; 进口java.io.ByteArrayOutputStream; 进口java.io.IOException; 进口java.io.InputStream; 公开课StreamTool {/* *从数据流中获得数据 */公共静态byte [] readInputStream (InputStream InputStream)抛出IOException { byte[]新字节缓冲区=[1024]; int len=0; ByteArrayOutputStream bos=new ByteArrayOutputStream (); 在((len=read(缓冲))!=1){ bos。写(缓冲区,0,len); } bos.close (); 返回bos.toByteArray (); } } >之前5:程序中用到的字符串资源字符串。xml里的内容
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; resources> & lt;字符串名称=癶ello”在hello World, MainActivity ! & lt;/string> & lt;字符串名称=" app_name "祝辞图片浏览器& lt;/string> & lt;字符串名称=皍rlpath”在网络图片地址& lt;/string> & lt;字符串名称="按钮"祝辞显示& lt;/string> & lt;字符串名称=按砦蟆钡脑谕缌映? lt;/string> & lt;/resources> >之前6:程序布局文件主要。xml的内容
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android:取向=按怪薄? android: layout_width="宽和" android: layout_height="宽和" 比; & lt; TextView android: layout_width="宽和" android: layout_height=" wrap_content " android:文本=" @string/urlpath”/比; EditText & lt; android: id=癅 + id/urlpath” android: layout_width="宽和" android: layout_height=" wrap_content " android:文本=" http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg "/比; & lt;按钮 android: id=癅 + id/按钮” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本=" @string/按钮”/比; & lt; ImageView android: id=癅 + id/imageView” android: layout_width=" wrap_content " android: layout_height=" wrap_content "/比; & lt;/LinearLayout>Android从网络中获得一张图片并显示在屏幕上的实例详解