首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

【Android】Web开发之展示网络图片的两种方法

2013-10-23 
【Android】Web开发之显示网络图片的两种方法根据URL返回一个位图式 package com.app.mywebimport java.io.

【Android】Web开发之显示网络图片的两种方法
根据URL返回一个位图格式 

package com.app.myweb;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URI;import java.net.URL;import java.net.URLConnection;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class ShowWebPicture extends Activity{private Button button1,button2;private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.showwebpicture);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);imageView = (ImageView) findViewById(R.id.imageView1);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubimageView.setImageBitmap(getBitMap("http://10.0.2.2:8888/android/image01.jpg"));}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubimageView.setImageDrawable(getDrawable("http://10.0.2.2:8888/android/image02.jpg"));}});}/** 根据URL返回一个位图格式 */public Bitmap getBitMap(String strUrl) {Bitmap bitmap = null;InputStream is = null;try {URL url = new URL(strUrl);URLConnection conn = url.openConnection();is = conn.getInputStream();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}bitmap = BitmapFactory.decodeStream(is);return bitmap;}/** 根据URL返回Drawable */public Drawable getDrawable(String strUrl) {Drawable drawable = null;InputStream is = null;try {URL url = new URL(strUrl);URLConnection conn = url.openConnection();is = conn.getInputStream();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}drawable = Drawable.createFromStream(is, "src");return drawable;} }


热点排行