//==========================用了两种方法======================
1.------------用流方式
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[100];
//设置写入路径以及图片名称
OutputStream bos = new FileOutputStream(new File( "C:\\thetempimg.gif"));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close();
//关闭输出流
b=true;
} catch (Exception e) {
//如果图片未找到
b=false;
}
int[] a = new int[2];
if(b){ //图片存在
//得到文件
java.io.File file = new java.io.File("C:\\thetempimg.gif");
[color=darkblue]BufferedImage bi = null;[/color]
try {
//读取图片
[color=blue] bi = javax.imageio.ImageIO.read(file);[/color]
} catch (IOException ex) {
ex.printStackTrace();
}
a[0] = bi.getWidth(); //获得 宽度
a[1] = bi.getHeight(); //获得 高度
//删除文件
file.delete();
}else{ //图片不存在
a=null;
}
return a;
}
// 显示网络上的图片
public static Bitmap returnBitMap(String url) {
Log.i("returnBitMap","url="+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;
}
1.------------用流方式
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[100];
//设置写入路径以及图片名称
OutputStream bos = new FileOutputStream(new File( "C:\\thetempimg.gif"));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close();
//关闭输出流
b=true;
} catch (Exception e) {
//如果图片未找到
b=false;
}
int[] a = new int[2];
if(b){ //图片存在
//得到文件
java.io.File file = new java.io.File("C:\\thetempimg.gif");
[color=darkblue]BufferedImage bi = null;[/color]
try {
//读取图片
[color=blue] bi = javax.imageio.ImageIO.read(file);[/color]
} catch (IOException ex) {
ex.printStackTrace();
}
a[0] = bi.getWidth(); //获得 宽度
a[1] = bi.getHeight(); //获得 高度
//删除文件
file.delete();
}else{ //图片不存在
a=null;
}
return a;
}
// 显示网络上的图片
public static Bitmap returnBitMap(String url) {
Log.i("returnBitMap","url="+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;
}