你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 图像系列: 将本地图片加载到Drawable

[日期:2012-09-01] 来源:Linux社区  作者:knowheart [字体:大 中 小]

Android 图像系列: 将本地图片加载到Drawable

  1. /** 
  2.      * 将文件生成位图 
  3.      * @param path 
  4.      * @return 
  5.      * @throws IOException 
  6.      */  
  7.     public BitmapDrawable getImageDrawable(String path)  
  8.         throws IOException  
  9.     {  
  10.         //打开文件   
  11.         File file = new File(path);  
  12.         if(!file.exists())  
  13.         {  
  14.             return null;  
  15.         }  
  16.           
  17.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  18.         byte[] bt = new byte[BUFFER_SIZE];  
  19.           
  20.         //得到文件的输入流   
  21.         InputStream in = new FileInputStream(file);  
  22.           
  23.         //将文件读出到输出流中   
  24.         int readLength = in.read(bt);  
  25.         while (readLength != -1) {  
  26.             outStream.write(bt, 0, readLength);  
  27.             readLength = in.read(bt);  
  28.         }  
  29.           
  30.         //转换成byte 后 再格式化成位图   
  31.         byte[] data = outStream.toByteArray();  
  32.         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// 生成位图   
  33.         BitmapDrawable bd = new BitmapDrawable(bitmap);  
  34.           
  35.         return bd;  
  36.     }  
linux
相关资讯       Android入门教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事��刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款