android本地图片转bitmap,Android中图片的网络路径转换为Bitmap格式
在Android开发中,常常会需要将一张网络图片转换为Bitmap格式,为什么这样做呢,那是因为很多函数要求的是Bitmap类型的参数,所以直接传字符串进去是不行的,这时候就需要转换一下,源码如下:public Bitmap returnBitMap(String url) {URL myFileUrl = null;Bitmap bitmap = null;try {myFileUrl = ne
在Android开发中,常常会需要将一张网络图片转换为Bitmap格式,为什么这样做呢,那是因为很多函数要求的是Bitmap类型的参数,所以直接传字符串进去是不行的,这时候就需要转换一下,源码如下:
public Bitmap returnBitMap(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.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
特备份在此,方便以后查阅。
更多推荐
所有评论(0)