Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android 中顯示網絡圖片的實現

Android 中顯示網絡圖片的實現

編輯:初級開發

package com.jinyan.image;

  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.util.Log;

  import android.view.VIEw;

  import android.view.VIEw.OnClickListener;

  import android.widget.Button;

  import android.widget.ImageVIEw;

  public class ActivityMain extends Activity {

  /** Called when the activity is first created. */

  String imageUrl = "/School/UploadFiles_7810/201106/20110609222044106.png";

  Bitmap bmImg;

  ImageView imVIEw;

  Button button1;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  imView = (ImageView) findViewById(R.id.imvIEw);

  imVIEw.setImageBitmap(returnBitMap(imageUrl));

  }

  public Bitmap returnBitMap(String url) {

  URL myFileUrl = null;

  Bitmap bitmap = null;

  try {

  myFileUrl = new URL(url);

  } catch (MalformedURLException e) {

  e.printStackTrace();

  }

  try {

  HttpURLConnection conn = (HttpURLConnection) myFileUrl

  .openConnection();

  conn.setDoInput(true);

  conn.connect();

  InputStream is = conn.getInputStream();

  bitmap = BitmapFactory.decodeStream(is);

  is.close();

  } catch (IOException e) {

  e.printStackTrace();

  }

  return bitmap;

  }

  }

  注意,想要讓你的程序 可以訪問網絡,你必須在menifest文件裡邊增加:

  < uses-permission android:name="android.permission.INTERNET" />

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved