public void saveBitmapFile(Bitmap bitmap){
File file=new File(ContactService.getImageURI(Environment.getExternalStorageDirectory()+"/pic/01.jpg");//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 从网络上获取图片,如果图片在本地存在的话就直接拿,如果不存在再去服务器上下载图片
* 这里的path是图片的地址
*/
public static Uri getImageURI(String localPath, String path) throws Exception {
File file = new File(localPath);
// 如果图片存在本地缓存目录,则不去服务器下载
if (file.exists()) {
return Uri.fromFile(file);//Uri.fromFile(path)这个方法能得到文件的URI
} else {
// 从网络上获取图片
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}
return null;
}
//BitmapFactory.方法中很多生成bitmap的方法
BitmapFactory.decodeFile(filePath, options);