Android从网络中获得一张图片并显示在屏幕上的实例详解
程序员文章站
2023-11-28 23:45:10
android从网络中获得一张图片并显示在屏幕上的实例详解
看下实现效果图:
1:androidmanifest.xml的内容
android从网络中获得一张图片并显示在屏幕上的实例详解
看下实现效果图:
1:androidmanifest.xml的内容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.capinftotech.image" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion="8" /> <uses-permission android:name="android.permission.internet" /> </manifest>
注意访问网络中的数据需要添加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里的内容
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello world, mainactivity!</string> <string name="app_name">图片浏览器</string> <string name="urlpath">网络图片地址</string> <string name="button">显示</string> <string name="error">网络连接超时</string> </resources>
6:程序布局文件main.xml的内容
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/urlpath" /> <edittext android:id="@+id/urlpath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg" /> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> <imageview android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
以上使用android 获取网路图片并显示的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!