Android用ImageView显示本地和网上的图片

news/2024/5/18 12:57:26 标签: Android, BBS, UI, Blog, XML

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;
}
转载请标明出处:3G Study :http://blog.3gstdy.com/archives/38


http://www.niftyadmin.cn/n/593956.html

相关文章

iOS 即时视频和聊天(基于环信)

先上效果图: 屏幕快照 2015-07-30 下午5.19.46.png说说需求:开发一个可以进行即时视频聊天软件. 最近比较忙,考完试回到公司就要做这个即时通信demo.本来是打算用xmpp协议来做视频通信的,想了想要搞后台,还要搭建服务器.一开始没明白是怎么样的一种形式.(现在想了想,其实就是自…

react native组件的生命周期

react native组件的生命周期 一、当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后,在第一次绘制 render() 之前。可以在这里做一些业务初始化操…

电信网通证实台湾地震影响内地访问国际网站(12月27日)

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>电信网通证…

java link 使用_java集合之Link的比较

第1部分 List概括先回顾一下List的框架图(01) List 是一个接口&#xff0c;它继承于Collection的接口。它代表着有序的队列。(02) AbstractList 是一个抽象类&#xff0c;它继承于AbstractCollection。AbstractList实现List接口中除size()、get(int location)之外的函数。(03) …

百度人为操纵搜索结果遭曝光 品牌总监离职

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>百度人为操…

有空的时候请大家去给《Web性能测试实战》投票吧。

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>请各位兄弟…

用flask编写自己的博客(1)

照着视频写代码&#xff01;因为底子原因&#xff0c;进度太慢&#xff0c;对其中的部分代码和知识点进行记录 一、设计models&#xff1a; ​ 本着简单的原则设计&#xff0c;只包函user、post、comments 三个表格&#xff0c;user为用户信息&#xff0c;post为文章列表&#…

Android之浮动小窗口

//创建创建全局变量类 1 public class MyApplication extends Application {2 3 /**4 * 创建全局变量 5 * 全局变量一般都比较倾向于创建一个单独的数据类文件&#xff0c;并使用static静态变量 6 * 7 * 这里使用了在Application中添加数据的方法实现全局变量 8 * 注意…