//本地图片文件加载方法
/**
* 加载本地图片
*
* @param url
* 本地图片文件物理地址
* @return Bitmap
*/
private Bitmap getLocalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
//网络图片加载方法
/**
* 加载网络图片
*
* @param url
* 网络图片地址
* @return Bitmap
*/
private Bitmap getHttpBitmap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
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;
}
//加载网络图片之前不要忘记在工程配置文件中添加权限
<uses-permission android:name="android.permission.INTERNET" />
//使用相应方法
mImageView = (ImageView) findViewById(R.id.image);
mBitmap = getLocalBitmap("/sdcard/DCIM/100ANDRO/1.jpg");
mBitmap = getHttpBitmap("http://www.google.com.sg/intl/zh-CN_cn/images/logos/images_logo_lg.gif");
mImageView.setImageBitmap(mBitmap);