package com.aib.soft;

 import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log; public class Util { /*
* 根据url从网络中获取一张图片
*
*/
public static Bitmap getHttpBitmap(String url) { URL myFileURL; Bitmap bitmap = null; try { myFileURL = new URL(url); // 获得连接 HttpURLConnection conn = (HttpURLConnection) myFileURL
.openConnection(); // 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制 conn.setConnectTimeout(60000); // 连接设置获得数据流 conn.setDoInput(true); // 不使用缓存 conn.setUseCaches(false); // 这句可有可无,没有影响 // conn.connect(); // 得到数据流 InputStream is = conn.getInputStream(); // 解析得到图片 bitmap = BitmapFactory.decodeStream(is); // 关闭数据流 is.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; } /**
* Save Bitmap to a file.保存图片到SD卡。
*
* @param bitmap
* @param file
* @return error message if the saving is failed. null if the saving is
* successful.
* @throws IOException
*/
public static void saveBitmapToFile(Bitmap bitmap, String _file)throws IOException {
BufferedOutputStream os = null;
try {
// Log.e("aib", "保存图片开始");
File file = new File(_file);
// String _filePath_file.replace(File.separatorChar +
// file.getName(), "");
int end = _file.lastIndexOf(File.separator);
String _filePath = _file.substring(0, end);
File filePath = new File(_filePath);
if (!filePath.exists()) {
filePath.mkdirs();
}
// Log.e("aib", "已创建目录");
file.createNewFile();
// Log.e("aib", "createNewFile()");
os = new BufferedOutputStream(new FileOutputStream(file));
// Log.e("aib", "new BufferedOutputStream");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
// Log.e("aib", "保存图片结束");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e("aib", e.getMessage(), e);
}
}
}
}
}

从网络上获取图片并保存在sdCard上的更多相关文章

  1. ios 从网络上获取图片并在UIImageView中显示

    ios 从网络上获取图片   -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...

  2. 【iOS开发-从网络上获取图片尺寸】

    实际开发过程中,容易碰到从网络上获取图片尺寸的场景,比如一个UIImageView要装载从网络上获取的图片,但要先设置其frame,此时又不知道图片尺寸,就要从网络上获取尺寸了.为了最好的用户体验,一 ...

  3. Android 多线程:使用Thread和Handler (从网络上获取图片)

    当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分 ...

  4. 从网络中获取图片显示到Image控件并保存到磁盘

    一.从网络中获取图片信息: /// <summary> /// 获取图片 /// </summary> /// <param name="url"&g ...

  5. php canvas 前端JS压缩,获取图片二进制流数据并上传

    <?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ //二进制数据流 $data = file_get_c ...

  6. word文档的图片怎么保存到ueditor上

    word图片转存,是指UEditor为了解决用户从word中复制了一篇图文混排的文章粘贴到编辑器之后,word文章中的图片数据无法显示在编辑器中,也无法提交到服务器上的问题而开发的一个操作简便的图片转 ...

  7. 从网络上获取图片,并写入excel文件

    package com.weChat.utils; import com.manage.utils.DateUtil;import com.manage.utils.MD5Util;import or ...

  8. ios 从网络上获取图片

    -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIImage * result; N ...

  9. iOS 通过网络请求获取图片的下载歌曲

    1.导入代理<NSURLConnectionDataDelegate> @interface ViewController ()<NSURLConnectionDataDelegat ...

随机推荐

  1. BIEE在creating domain步骤停止的解决的方法

    1.错误现象: biee11g creating domain  csf entries will not be parsed since the adminserver is unreachable ...

  2. jsp页面onsubmit=&quot;return checklogin();&quot;该解决方案给

    选择Window->Preferences->MyEclipse->Validation 去掉方框里的对号,然后Apply 然后点击Yes->然后再点击ok->Yes,足 ...

  3. 使用 Eclipse 的 SVN 主要插件创建项目/支/标签

    原文 阅读 Mark Phippard 该博客以及<Subversion 与版本号控制>之后,我了解到 分支/标签 是 SVN 非常棒的特性之中的一个.但我在使用推荐的 "tru ...

  4. SettingsProvider该CRUD

    转载请注明出处:http://blog.csdn.net/droyon/article/details/35558697 什么时候delete要么update时间.需要清空缓存并重新加载数据. 1.i ...

  5. CocoaPods 建立私有仓库

    CocoaPods是iOS,Mac下优秀的第三方包管理工具,类似于java的maven,给我们项目管理带来了极大的方便. [个人或公司在开发过程中,会积累很多可以复用的代码包,有些我们不想开源,又想像 ...

  6. 【百度地图API】让用户选择起点和终点的驾车导航

    原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...

  7. Lucene.Net简介和分词

    Lucene.net站内搜索—2.Lucene.Net简介和分词 2015-03-24 23:10 by 邹琼俊, 118 阅读, 1 评论, 收藏, 编辑 Lucene.Net简介 Lucene.N ...

  8. Asp.Net MVC5入门学习系列③

    原文:Asp.Net MVC5入门学习系列③ 添加一个视图(View) 接着上篇的入门系列,上面解说添加一个简单Controller(控制器),这里我们简单的在来添加一个View(视图)来展示我们Co ...

  9. 小结php中几种网页跳转

    1.使用网页中<a href=.....></a>实现跳转: 2.<form action="php_request2.php" method=&qu ...

  10. MySQL中group_concat函数-和group by配合使用

    MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...