Android使用HttpURLConnection显示网络图片

发布时间:2016-12-8 8:33:05 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Android使用HttpURLConnection显示网络图片",主要涉及到Android使用HttpURLConnection显示网络图片方面的内容,对于Android使用HttpURLConnection显示网络图片感兴趣的同学可以参考一下。

package android.test; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; public class Main extends Activity { private ImageView m_imgView; private String m_strImgURL = "http://www.lvyou114.com/member/1971/UploadFile/200691217738739.jpg"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_imgView = (ImageView) findViewById(R.id.myImageView); m_imgView.setImageBitmap(GetURLBitmap()); } private Bitmap GetURLBitmap() { URL url = null; Bitmap bmp = null; try { url = new URL(m_strImgURL); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); bmp = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e) { e.printStackTrace(); } return bmp; } } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.test" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7"></uses-sdk> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" 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-permission android:name="android.permission.INTERNET"/> </manifest>

上一篇:JsonUtilsExtension
下一篇:exit(0)与exit(1)、return区别

相关文章

相关评论