图片资源类型转换为bitmap
2015-10-27 12:07
302 查看
1、网络图片:
背景:最新的SDK,不允许在main线程里面执行网络操作,否则报错:NetworkOnMainThreadException。
解决:在main线程中另开一个线程,进行相应的网络操作,再使用handler异步操作主线程的UI更新。
代码:
2、本地图片(sdcard):
背景:最新的SDK,不允许在main线程里面执行网络操作,否则报错:NetworkOnMainThreadException。
解决:在main线程中另开一个线程,进行相应的网络操作,再使用handler异步操作主线程的UI更新。
代码:
public class BitmapimgActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bitmapimg); imageView = (ImageView) this.findViewById(R.id.imgs); new Thread() { public void run() { // 图片资源 String url = "http://start.firefoxchina.cn/img/worldindex/logo.png"; // 得到可用的图片 Bitmap bitmap = getHttpBitmap(url); Message msg=new Message(); msg.what=1; msg.obj=bitmap; handler.sendMessage(msg); // imageView.setBackgroundResource(R.drawable.ic_launcher); }; }.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { int s = msg.what; if(s==1){ Bitmap bits=(Bitmap) msg.obj; // 显示 imageView.setImageBitmap(bits); } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.bitmapimg, menu); return true; } /** * 获取网落图片资源 * * @param url * @return */ public static Bitmap getHttpBitmap(String url) { URL myFileURL; Bitmap bitmap = null; try { myFileURL = new URL(url); // 获得连接 HttpURLConnection conn = (HttpURLConnection) myFileURL .openConnection(); // 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制 conn.setConnectTimeout(6000); // 连接设置获得数据流 conn.setDoInput(true); // 不使用缓存 conn.setUseCaches(false); // 这句可有可无,没有影响 // conn.connect(); // 得到数据流 InputStream is = conn.getInputStream(); // 报错??????????????? // 解析得到图片 bitmap = BitmapFactory.decodeStream(is); // 关闭数据流 is.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; } }
2、本地图片(sdcard):
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView image1 = (ImageView) findViewById(R.id.iv1); //获得ImageView对象 /*为什么图片一定要转化为 Bitmap格式的!! */ Bitmap bitmap = getLoacalBitmap("/sdcard/tubiao.jpg"); //从本地取图片(在cdcard中获取) // image1 .setImageBitmap(bitmap); //设置Bitmap } /** * 加载本地图片 * @param url * @return */ public static Bitmap getLoacalBitmap(String url) { try { FileInputStream fis = new FileInputStream(url); return BitmapFactory.decodeStream(fis); ///把流转化为Bitmap图片 } catch (FileNotFoundException e) { e.printStackTrace(); return null; } }
相关文章推荐
- 分治算法: 归并排序(详解)
- poj 3270 更换使用
- UILabel自适应内容高度总结
- 写外键
- mysq索引类别
- MyBatis学习 ——调用存储过程
- Jetty与tomcat的比较
- MarkdownPad 汉化破解(含下载地址)
- 5.Python常用流程控制及其他语句
- 51nod 1068 Bash游戏 V3 (找规律)
- JS/JQ控制音频的播放和暂停
- php语言中使用json的技巧及json的实现代码详解
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- mysql索引
- CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14
- 将一个int数组变为一个int整数
- Linux系统上安装MRTG来监控网络流量
- 32位正常,64位随机崩溃问题
- boost 字符串处理(1)
- 用dnSpy破解某旅游系统5.2版。