最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法

 更新時(shí)間:2013年04月18日 17:19:22   作者:  
本篇文章小編為大家介紹,Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法,需要的朋友參考下

我們的應(yīng)用或多或少都會(huì)從網(wǎng)絡(luò)獲取圖片數(shù)據(jù)然后進(jìn)行顯示,下面就將實(shí)現(xiàn)一個(gè)這樣的例子,獲取網(wǎng)絡(luò)中的圖片!

首先:我們來(lái)看一下效果圖

界面中有三個(gè)控件,一個(gè)EditText,一個(gè)Button,一個(gè)ImageView

1、下面是具體布局文件

<EditText
  android:id="@+id/picturepagh"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello_world" />

<Button
  android:id="@+id/btn"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="查看" />

<ImageButton
  android:id="@+id/imageView"
  android:layout_width="fill_parent"
  android:layout_height="200px" />

2、在MainActivity中進(jìn)行圖片圖示代碼編寫(xiě)

public class MainActivity extends Activity {
 private Button btn;
 private EditText path;
 private ImageView imgview;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn = (Button) findViewById(R.id.btn);
  path = (EditText) findViewById(R.id.picturepagh);
  imgview = (ImageView) findViewById(R.id.imageView);

  btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Log.i("CLICK", ((Button) v).getText().toString());
    new Thread(runa).start();
   }
  });
 }

 public void setView() {
  String picturepath = path.getText().toString();
  byte[] data = null;
  try {
   data = ImageService.getImage(picturepath);
   Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:圖片工廠!
   Looper.prepare();// 必須調(diào)用此方法,要不然會(huì)報(bào)錯(cuò)
   Message msg = new Message();
   msg.what = 0;
   msg.obj = bitmap;
   handler.sendMessage(msg);
  } catch (Exception e) {
   Toast.makeText(getApplicationContext(), "獲取圖片錯(cuò)誤", 1).show();
  }
 }

 private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   if (msg.what == 0) {
    updateImageView((Bitmap) msg.obj);
   }
  }

 };

 private Runnable runa = new Runnable() {
  @Override
  public void run() {
   setView();
  }
 };

 private void updateImageView(Bitmap bm) {
  imgview.setImageBitmap(bm);
 }
}

 3、添加一個(gè)ImageService圖片服務(wù)類(lèi),里面包含一個(gè)獲取網(wǎng)絡(luò)數(shù)據(jù)的方法;

public class ImageService {

 // 獲取網(wǎng)絡(luò)圖片的數(shù)據(jù)
 public static byte[] getImage(String picturepath) throws Exception {
  URL url = new URL(picturepath);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于http協(xié)議的連接對(duì)象
  conn.setConnectTimeout(10);// 10秒;
  conn.setRequestMethod("GET");// 大寫(xiě)
  if (conn.getResponseCode() == 200) {
   InputStream ins = conn.getInputStream();
   return StreamTool.read(ins);
  }
  return null;
 }
}

 4、添加一個(gè)流處理工作類(lèi)StreamTool

public class StreamTool {

 public static byte[] read(InputStream ins) throws Exception {
  ByteArrayOutputStream outstream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int length = 0;
  while ((length = ins.read(buffer)) > -1) {
   outstream.write(buffer, 0, length);
  }
  outstream.close();
  return outstream.toByteArray();
 }
}

 5、大功告成?NO,還要添加網(wǎng)絡(luò)訪問(wèn)權(quán)限: <uses-permission android:name="android.permission.INTERNET" />

 OK,運(yùn)行程序!

 

相關(guān)文章

最新評(píng)論

新巴尔虎左旗| 汉源县| 乳山市| 荔浦县| 孙吴县| 大港区| 五河县| 盐亭县| 阿坝| 华坪县| 洪湖市| 西和县| 新竹县| 岳池县| 无为县| 前郭尔| 拜泉县| 当雄县| 韩城市| 灵武市| 屏南县| 凯里市| 龙南县| 开原市| 长寿区| 苍梧县| 靖远县| 临江市| 克东县| 平乡县| 靖江市| 西丰县| 兖州市| 汨罗市| 牙克石市| 鹤庆县| 彰化市| 喀喇沁旗| 浙江省| 太和县| 富顺县|