Android使用线程获取网络图片的方法
本文为大家分享了Android使用线程获取网络图片的具体代码,供大家参考,具体内容如下
AndroidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.zdcrobot.handlermessage"> <uses-permissionandroid: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> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <android.support.design.widget.CoordinatorLayoutxmlns: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
packagecom.zdcrobot.handlermessage; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.support.v7.app.AppCompatActivity; importandroid.view.View; importandroid.widget.Button; importandroid.widget.ImageView; importjava.io.IOException; importjava.io.InputStream; importjava.net.HttpURLConnection; importjava.net.MalformedURLException; importjava.net.URL; publicclassMainActivityextendsAppCompatActivity{ privateButtonbutton; privateImageViewimageView; privateStringimagPath="http://pica.nipic.com/2007-11-09/200711912453162_2.jpg"; privatefinalintIS_FINISH=1; privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ Bitmapbitmap=(Bitmap)msg.obj; imageView.setImageBitmap(bitmap); } }; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); imageView=(ImageView)findViewById(R.id.image1); button.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ newThread(newMyClass()).start(); } }); } publicclassMyClassimplementsRunnable{ @Override publicvoidrun(){ Bitmapbitmap=null; try{ URLurl=newURL(imagPath); HttpURLConnectionhttpURLConnection=(HttpURLConnection)url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.connect(); InputStreaminputStream=httpURLConnection.getInputStream(); bitmap=BitmapFactory.decodeStream(inputStream); }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } Messagemessage=Message.obtain(); message.obj=bitmap; message.what=IS_FINISH; handler.sendMessage(message); } } }
以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。