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     }
  • 相关阅读:
    CTF -攻防世界-crypto新手区(5~11)
    CTF密码学常见加密解密总结
    CTF -攻防世界-crypto新手区(1~4)
    跨域问题解决
    npm使用记录
    dva控制元素动态消失隐藏
    maven安装记录
    postgreSQL记录
    关于System.__ComObject一些问题
    论文中表格跨页处理
  • 原文地址:https://www.cnblogs.com/renzimu/p/4539274.html
Copyright © 2011-2022 走看看