国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 網絡圖片查看顯示的實現方法

2020-04-11 12:29:39
字體:
來源:轉載
供稿:網友

我們的應用或多或少都會從網絡獲取圖片數據然后進行顯示,下面就將實現一個這樣的例子,獲取網絡中的圖片!

首先:我們來看一下效果圖

界面中有三個控件,一個EditText,一個Button,一個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中進行圖片圖示代碼編寫

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();// 必須調用此方法,要不然會報錯
   Message msg = new Message();
   msg.what = 0;
   msg.obj = bitmap;
   handler.sendMessage(msg);
  } catch (Exception e) {
   Toast.makeText(getApplicationContext(), "獲取圖片錯誤", 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、添加一個ImageService圖片服務類,里面包含一個獲取網絡數據的方法;

public class ImageService {

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

 4、添加一個流處理工作類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,還要添加網絡訪問權限: <uses-permission android:name="android.permission.INTERNET" />

 OK,運行程序!

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高阳县| 石林| 石台县| 宁武县| 寻甸| 成都市| 林甸县| 于都县| 来宾市| 石柱| 会宁县| 永寿县| 宁德市| 咸阳市| 枣庄市| 庆城县| 习水县| 固阳县| 读书| 康马县| 涪陵区| 札达县| 兴国县| 彭泽县| 嘉善县| 蓝田县| 和田市| 冷水江市| 隆回县| 遂宁市| 东城区| 兴山县| 密云县| 察雅县| 三门县| 余干县| 伊宁市| 吴川市| 象山县| 师宗县| 崇文区|