1.添加布局文件: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" > <ImageView
android:id="@+id/mImageView"
android:layout_width="300dp"
android:layout_height="300dp" /> <ProgressBar
android:id="@+id/mProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="10dip"
android:minHeight="10dip" /> <Button
android:id="@+id/btnGetImg"
android:layout_width="120dp"
android:layout_height="50dp"
android:text="Getmage" /> <Button
android:id="@+id/btnAbort"
android:layout_width="120dp"
android:layout_height="50dp"
android:text="Abort" /> </LinearLayout>

2.添加java文件MainActivity.java

 package com.example.loadimgfrominternet;

 import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar; public class MainActivity extends Activity implements OnClickListener { private Button btnGetImg;
private Button btnAbort;
private ProgressBar mProgressBar;
private ImageView mImageView;
private static final String ImageUrl = "https://images0.cnblogs.com/i/169207/201408/112229149526951.png"; ImageLoader loader = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
btnGetImg = (Button) findViewById(R.id.btnGetImg);
btnAbort = (Button) findViewById(R.id.btnAbort);
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
btnGetImg.setOnClickListener(this);
btnAbort.setOnClickListener(this); mProgressBar.setVisibility(View.INVISIBLE);
mImageView = (ImageView) findViewById(R.id.mImageView);
} @Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnGetImg) {
loader = new ImageLoader();
loader.execute(ImageUrl);
btnGetImg.setEnabled(false);
} else if (id == R.id.btnAbort) {
loader.cancel(true);
btnGetImg.setEnabled(true);
}
} class ImageLoader extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress();
mImageView.setImageResource(R.drawable.noimg);
super.onPreExecute();
} @Override
protected Bitmap doInBackground(String... params) {
String imgUri = params[]; try {
URL url = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
OutputStream outputStream = null;
String filename = "local_temp_image"; try { url = new URL(imgUri);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setConnectTimeout( * );
inputStream = conn.getInputStream();
outputStream = openFileOutput(filename,
Context.MODE_PRIVATE);
byte[] data = new byte[];
int seg = ;
long total = conn.getContentLength();
long current = ;
while (!isCancelled()
&& (seg = inputStream.read(data)) != -) {
outputStream.write(data, , seg);
current += seg;
int progress = (int) ((float) current / total * );
publishProgress(progress);// // 通知进度条UI更新
SystemClock.sleep();
}
} finally {
if (conn != null) {
conn.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
// return BitmapFactory.decodeStream(HandlerData(ImageUrl));
return BitmapFactory.decodeFile(getFileStreamPath(filename)
.getAbsolutePath());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
} @Override
protected void onProgressUpdate(Integer... values) {
if (isCancelled())
return;
mProgressBar.setProgress(values[]);
btnGetImg.setText(values[] + "%");
super.onProgressUpdate(values);
} @Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
mProgressBar.setVisibility(View.INVISIBLE);
mProgressBar.setProgress();
btnAbort.setEnabled(false);
super.onPostExecute(bitmap);
}
} /* 根据URI地址读取输入流 */
public static InputStream HandlerData(String url) {
InputStream inStream = null; try {
URL feedUrl = new URL(url);
URLConnection conn = feedUrl.openConnection();
conn.setConnectTimeout( * );
inStream = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} return inStream;
}
}

3.运行效果图如下:

4.附上项目下载地址:

http://files.cnblogs.com/_ymw/LoadImgFromInternet_%E5%8D%9A%E5%AE%A2%E9%99%84%E4%BB%B6.rar

最新文章

  1. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
  2. 有吧友需要PDF的下载站点,好吧,我这边汇总一下
  3. linux命令(3):pwd命令
  4. RSA 加解密转换
  5. 【转载】python super的用法
  6. LINK : fatal error LNK1104: 无法打开文件“gtestd.lib”
  7. 转:Spring中@Autowired注解、@Resource注解的区别
  8. STAD Parameters
  9. PS 颜色表大全-CMYK颜色表(2)
  10. /lib /usr/lib /usr/local/lib 区别
  11. HW--自守数
  12. zTree在Asp.Net中的使用
  13. Listview控件实现已选择效果
  14. ASP:GB2312格式文本文件转换成UTF-8格式
  15. MySQL中的完整性约束条件(主键、外键、唯一、非空)
  16. Twitter数据抓取的方法(三)
  17. WIN7 嵌入式系统安装教程 Windows Embedded Standard 2011 安装
  18. nexus(Maven仓库私服)的安装、配置、使用和仓库迁移
  19. Hadoop基础-Hadoop快照管理
  20. Python Object Oriented

热门文章

  1. Android EditText 限制输入字符
  2. codeforce580c (dfs)
  3. el-input怎么绑定回车事件
  4. 一个flink作业的调优
  5. 找jar包的网址
  6. [剑指Offer] 26.二叉搜索树与双向链表
  7. express框架 中间件
  8. 【bzoj2768/bzoj1934】[JLOI2010]冠军调查/[Shoi2007]Vote 善意的投票 最小割
  9. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT
  10. BZOJ1407 [Noi2002]Savage 【扩展欧几里得】