zoukankan      html  css  js  c++  java
  • Android 开发工具类 23_getImage

    pathText = "http://192.168.1.100:8080/ServerForPicture/wangjialin.jpg"

     1 import java.io.InputStream;
     2 import java.net.HttpURLConnection;
     3 import java.net.URL;
     4 
     5 import android.graphics.Bitmap;
     6 import android.graphics.BitmapFactory;
     7 
     8 public class ImageService {
     9     
    10     /**
    11      * 获取图片
    12      * @param path 图片路径
    13      * @return
    14      */
    15     public static Bitmap getImage(String path) throws Exception{
    16         
    17         URL url = new URL(path);
    18         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    19         conn.setConnectTimeout(5000);
    20         conn.setRequestMethod("GET");
    21         
    22         if(conn.getResponseCode() == 200){
    23             InputStream inStream = conn.getInputStream();
    24             Bitmap bitmap = BitmapFactory.decodeStream(inStream);
    25             return bitmap;
    26             /*byte[] data = StreamTool.read(inStream);
    27             Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    28             return bitmap;*/
    29         }
    30         return null;
    31     }
    32 
    33 }

    显示

     1 public void showimage(View v){
     2         String path = pathText.getText().toString();
     3         try {
     4             Bitmap bitmap = ImageService.getImage(path);
     5             imageView.setImageBitmap(bitmap);
     6         } catch (Exception e) {
     7             e.printStackTrace();
     8             Toast.makeText(getApplicationContext(), R.string.error, 1).show();
     9         }
    10     }
  • 相关阅读:
    页面生命周期
    设计模式
    算法
    window服务
    Xml
    sql声明变量,及if else语句、while语句的用法
    SQL 使用临时表和临时变量完成update表字段实际案例
    SQL Server遍历表的几种方法
    node快速构建express项目
    词法分析
  • 原文地址:https://www.cnblogs.com/renzimu/p/4539274.html
Copyright © 2011-2022 走看看