zoukankan      html  css  js  c++  java
  • Android用ImageView显示本地和网上的图片

    ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上。
    在UI xml定义一个ImageView如下:
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.myimage);
         ImageView image1 = (ImageView) findViewById(R.myImage.image);
         //Bitmap bitmap = getLoacalBitmap("/aa/aa.jpg"); //从本地取图片
         Bitmap bitmap =
    getHttpBitmap("http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg");
    //从网上取图片
         image1 .setImageBitmap(bitmap); //设置Bitmap
    }
    /**
    * 加载本地图片
    * http://bbs.3gstdy.com
    * @param url
    * @return
    */
    public static Bitmap getLoacalBitmap(String url) {
         try {
              FileInputStream fis = new FileInputStream(url);
              return BitmapFactory.decodeStream(fis);
         } catch (FileNotFoundException e) {
              e.printStackTrace();
              return null;
         }
    }
    /**
    * 从服务器取图片
    *http://bbs.3gstdy.com
    * @param url
    * @return
    */
    public static Bitmap getHttpBitmap(String url) {
         URL myFileUrl = null;
         Bitmap bitmap = null;
         try {
              Log.d(TAG, url);
              myFileUrl = new URL(url);
         } catch (MalformedURLException e) {
              e.printStackTrace();
         }
         try {
              HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
              conn.setConnectTimeout(0);
              conn.setDoInput(true);
              conn.connect();
              InputStream is = conn.getInputStream();
              bitmap = BitmapFactory.decodeStream(is);
              is.close();
         } catch (IOException e) {
              e.printStackTrace();
         }
         return bitmap;
    }
  • 相关阅读:
    FiddlerCoreAPI 使用简介
    fiddler script建议教程
    PDF文本内容批量提取到Excel
    pymc_实现贝叶斯统计模型和马尔科夫链蒙塔卡洛
    贝叶斯
    Logistic Ordinal Regression
    逻辑回归原理_挑战者飞船事故和乳腺癌案例_Python和R_信用评分卡(AAA推荐)
    逻辑回归实战--美国挑战者号飞船事故_同盾分数与多头借贷Python建模
    python操作mysql数据库
    多元回归比一元回归优越性
  • 原文地址:https://www.cnblogs.com/top5/p/2402277.html
Copyright © 2011-2022 走看看