Android http 下载图片
2015-08-06 10:41
591 查看
本文主要是介绍Android http下载图片操作,具体先看代码。
1.MainActivity.java
2.HttpUtils.java
3.布局文件activity_main.xml
4.修改配置文件AndroidManifest.xml,添加相应权限
1.MainActivity.java
public class MainActivity extends Activity implements OnClickListener { private String TAG="MainActivity"; private Bitmap mDownloadImage = null; private ImageView image = null; private downloadImageTask task; private boolean _isExe = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidgets(); task = new downloadImageTask(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); if (_isExe) { task.cancel(true); // 取消操作 } } private void initWidgets() { image = (ImageView) findViewById(R.id.img); Button btn = (Button) findViewById(R.id.download_btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.download_btn: if (!_isExe) { Toast.makeText(getApplicationContext(), "正在下载请稍后...", Toast.LENGTH_SHORT).show(); task.execute("http://f.hiphotos.baidu.com/image/w%3D2048/sign=3b06d28fc91349547e1eef6462769358/d000baa1cd11728b22c9e62ccafcc3cec2fd2cd3.jpg"); // 执行异步操作 _isExe = true; } break; default: break; } } class downloadImageTask extends AsyncTask<String, Integer, Boolean> { @Override protected Boolean doInBackground(String... params) { // TODO Auto-generated method stub Log.d(TAG,"[downloadImageTask->]doInBackground "+ params[0]); mDownloadImage = HttpUtils.getNetWorkBitmap(params[0]); return true; } // 下载完成回调 @Override protected void onPostExecute(Boolean result) { // TODO Auto-generated method stub image.setImageBitmap(mDownloadImage); Log.d(TAG,"result = " + result); super.onPostExecute(result); } // 更新进度回调 @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } } }
2.HttpUtils.java
/** * http工具类 http可以使用HttpURLConnection或HttpClient * */ public class HttpUtils { private static String TAG="HttpUtils"; /** * 获取网络图片 * * @param urlString * 如:http://f.hiphotos.baidu.com/image/w%3D2048/sign=3 * b06d28fc91349547e1eef6462769358 * /d000baa1cd11728b22c9e62ccafcc3cec2fd2cd3.jpg * @return */ public static Bitmap getNetWorkBitmap(String urlString) { URL imgUrl = null; Bitmap bitmap = null; try { imgUrl = new URL(urlString); // 使用HttpURLConnection打开连接 HttpURLConnection urlConn = (HttpURLConnection) imgUrl .openConnection(); urlConn.setDoInput(true); urlConn.connect(); // 将得到的数据转化成InputStream InputStream is = urlConn.getInputStream(); // 将InputStream转换成Bitmap bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block Log.d(TAG,"[getNetWorkBitmap->]MalformedURLException"); e.printStackTrace(); } catch (IOException e) { Log.d(TAG,"[getNetWorkBitmap->]IOException"); e.printStackTrace(); } return bitmap; } }
3.布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="24dip" android:layout_gravity="center" android:text="http下载图片测试 "/> <Button android:id="@+id/download_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载" /> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
4.修改配置文件AndroidManifest.xml,添加相应权限
<uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- 通过 http 协议上传文件
- Android 判断用户2G/3G/4G移动数据网络
- TCP/IP网络知识点总结
- 南邮 OJ 1628 销售网络问题
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- 通过/proc/sys/net/ipv4/优化Linux下网络性能
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)
- 服务器-TCP 在 EPOLL 模型中的注意细节
- 浅谈HTTP中Get与Post的区别
- Java / Android 基于Http的多线程下载的实现
- tmux(cp:https://wiki.freebsdchina.org/software/t/tmux)
- tmux(终端方便使用cp:http://wowubuntu.com/tmux.html)
- 发红包也要上税了?!传网络红包税收新规将出炉
- set specital termimal(https://wiki.archlinux.org/index.php/Color_Bash_Prompt#List_of_colors_for_pro)
- http获取网络资源文件等并保存到本地sd或者磁盘
- 一个相对健壮的node 静态http服务器
- php笔记07:http响应详解(禁用缓存设置和文件下载)
- 【网络测试】IPV4协议族
- HTTP常用对照表(content-type、HTML转义字符、ASCII、TCP常用端口等)
- Node.js开发入门—HTTP文件服务器