Android使用线程获取网络图片的方法
程序员文章站
2024-03-02 15:16:40
本文为大家分享了android使用线程获取网络图片的具体代码,供大家参考,具体内容如下
androidmanifest.xml ...
本文为大家分享了android使用线程获取网络图片的具体代码,供大家参考,具体内容如下
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zdcrobot.handlermessage"> <uses-permission android:name="android.permission.internet"></uses-permission> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:context="com.zdcrobot.handlermessage.mainactivity"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载图片"/> <imageview android:id="@+id/image1" android:layout_width="match_parent" android:layout_height="500dp" /> </linearlayout> </android.support.design.widget.coordinatorlayout>
mainactivity.class
package com.zdcrobot.handlermessage; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import android.widget.imageview; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; public class mainactivity extends appcompatactivity { private button button; private imageview imageview; private string imagpath = "http://pica.nipic.com/2007-11-09/200711912453162_2.jpg"; private final int is_finish = 1; private handler handler = new handler(){ @override public void handlemessage(message msg) { bitmap bitmap = (bitmap)msg.obj; imageview.setimagebitmap(bitmap); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button = (button)findviewbyid(r.id.button1); imageview = (imageview)findviewbyid(r.id.image1); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new thread(new myclass()).start(); } }); } public class myclass implements runnable{ @override public void run() { bitmap bitmap = null; try { url url = new url(imagpath); httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection(); httpurlconnection.setdoinput(true); httpurlconnection.connect(); inputstream inputstream = httpurlconnection.getinputstream(); bitmap = bitmapfactory.decodestream(inputstream); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } message message = message.obtain(); message.obj = bitmap; message.what = is_finish; handler.sendmessage(message); } } }
以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。
上一篇: RabbitMQ消息中间件示例详解
下一篇: mysql表的清空、删除和修改操作详解