andriod 读取网络图片
2016-02-29 15:03
405 查看
来自:/article/1352741.html
Android手机上,我们常用ImageView显示图片,我们本章获取网络图片并显示在ImageView中。
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imagephoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二、程序文件
打开“src/com.genwoxue.networkphoto/MainActivity.java”文件。
然后输入以下代码:
[java] view plain copy
package com.genwoxue.networkphoto;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imView = (ImageView) findViewById(R.id.imagephoto);
String imageUrl = "http://img.baidu.com/img/image/ilogob.gif";
new NetworkPhoto().execute(imageUrl);
}
/* 四个步骤:
* (1)onPreExecute(),执行预处理,它运行于UI线程,
* 可以为后台任务做一些准备工作,比如绘制一个进度条控件。
* (2)doInBackground(Params...),后台进程执行的具体计算在这里实现,
* doInBackground(Params...)是AsyncTask的关键,此方法必须重载。
* 在这个方法内可以使用 publishProgress(Progress...)改变当前的进度值。
* (3)onProgressUpdate(Progress...),运行于UI线程。如果
* 在doInBackground(Params...) 中使用了publishProgress(Progress...),就会
* 触发这个方法。在这里可以对进度条控件根据进度值做出具体的响应。
* (4)onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果
* 就是doInBackground(Params...)的返回值。此方法也要经常重载,如果Result为
* null表明后台任务没有完成(被取消或者出现异常)。 *
*/
//本案例我们仅使用了(2)和(4)
class NetworkPhoto extends AsyncTask<String, Integer, Bitmap> {
public NetworkPhoto() {
}
//doInBackground(Params...),后台进程执行的具体计算在这里实现,是AsyncTask的关键,此方法必须重载。
@Override
protected Bitmap doInBackground(String... urls) {
URL url = null;
Bitmap bitmap = null;
HttpURLConnection conn=null;
InputStream is=null;
try {
url = new URL(urls[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(conn!=null){
conn.disconnect();
conn=null;
}
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is=null;
}
}
return bitmap;
}
//onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果
//就是doInBackground(Params...)的返回值。
@Override
protected void onPostExecute(Bitmap bitmap) {
// 返回结果bitmap显示在ImageView控件
imView.setImageBitmap(bitmap);
}
}
}
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.genwoxue.networkphoto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.genwoxue.networkphoto.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意:需要在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
Android手机上,我们常用ImageView显示图片,我们本章获取网络图片并显示在ImageView中。
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imagephoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二、程序文件
打开“src/com.genwoxue.networkphoto/MainActivity.java”文件。
然后输入以下代码:
[java] view plain copy
package com.genwoxue.networkphoto;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imView = (ImageView) findViewById(R.id.imagephoto);
String imageUrl = "http://img.baidu.com/img/image/ilogob.gif";
new NetworkPhoto().execute(imageUrl);
}
/* 四个步骤:
* (1)onPreExecute(),执行预处理,它运行于UI线程,
* 可以为后台任务做一些准备工作,比如绘制一个进度条控件。
* (2)doInBackground(Params...),后台进程执行的具体计算在这里实现,
* doInBackground(Params...)是AsyncTask的关键,此方法必须重载。
* 在这个方法内可以使用 publishProgress(Progress...)改变当前的进度值。
* (3)onProgressUpdate(Progress...),运行于UI线程。如果
* 在doInBackground(Params...) 中使用了publishProgress(Progress...),就会
* 触发这个方法。在这里可以对进度条控件根据进度值做出具体的响应。
* (4)onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果
* 就是doInBackground(Params...)的返回值。此方法也要经常重载,如果Result为
* null表明后台任务没有完成(被取消或者出现异常)。 *
*/
//本案例我们仅使用了(2)和(4)
class NetworkPhoto extends AsyncTask<String, Integer, Bitmap> {
public NetworkPhoto() {
}
//doInBackground(Params...),后台进程执行的具体计算在这里实现,是AsyncTask的关键,此方法必须重载。
@Override
protected Bitmap doInBackground(String... urls) {
URL url = null;
Bitmap bitmap = null;
HttpURLConnection conn=null;
InputStream is=null;
try {
url = new URL(urls[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(conn!=null){
conn.disconnect();
conn=null;
}
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is=null;
}
}
return bitmap;
}
//onPostExecute(Result),运行于UI线程,可以对后台任务的结果做出处理,结果
//就是doInBackground(Params...)的返回值。
@Override
protected void onPostExecute(Bitmap bitmap) {
// 返回结果bitmap显示在ImageView控件
imView.setImageBitmap(bitmap);
}
}
}
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.genwoxue.networkphoto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.genwoxue.networkphoto.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意:需要在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- http请求
- maven 单元测试 ( http://www.cnblogs.com/qinpengming/p/5225380.html )
- SSL的TCP通信
- 用nodejs搭建最简单、轻量化的http server
- 移动端H5页面的设计稿尺寸大小规范-转载自http://www.chinaz.com/design/2015/1103/465670.shtml
- 解决apache启动错误"httpd:Could not reliably determine..."
- 数据包分析——数据链路层 和 网络层
- MATLAB主要特性http://cn.mathworks.com/products/matlab/
- 用CornerStone配置SVN,HTTP及svn简单使用说明
- linux之网络配置相关
- 安卓HTTP请求之get和post方法。
- gulp ( http://markpop.github.io/2014/09/17/Gulp入门教程 )
- iOS开发网络篇—数据缓存
- less 官网讲解 ( http://www.bootcss.com/p/lesscss/ )
- tomcat https ssl设置
- C# 网络打印机ESC指令打印小票
- TCP/IP
- Linux设置网络连接
- Netmap 高性能网络I/O框架
- 其实学校的网络就是绑定了地址所以对方使用在线就可以同时占用