为了账号安全,请及时绑定邮箱和手机立即绑定

异步任务实现网上下载图片并存储在sdcard上,并将下载的图片显示在屏幕上

标签:
Android

1.布局文件

[代码]xml代码:

?

1

2

3

4

5

<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">

 

    <button android:="download"   android:layout_width="match_parent" android:layout_height="wrap_content"   android:text="图片下载">

    <imageview android:id="@+id/iv"   android:layout_height="wrap_content" android:layout_width="wrap_content"   android:layout_gravity="center">

</imageview></button></linearlayout>

 

2.MainActivity.java

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

package com.example.day12_ex_01;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import android.app.Activity;

import android.app.ProgressDialog;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

    ProgressDialog pd;

    @Override

    protected void onCreate(Bundle   savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        pd=new ProgressDialog(this);

        pd.setTitle("图片下载");

        pd.setMessage("正在下载.....");

        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        pd.setIndeterminate(false);

        pd.setCancelable(false);

        pd.setMax(100);

     }

 

    public void download(View   view){

        pd.show();

        new MyTask().execute();

    }

    class MyTask extends AsyncTask<void,integer,void>{

 

        @Override

        protected   Void doInBackground(Void... params) {

             try   {

                URL   url=new URL("http://www.ytmfdw.com/image/img3.jpg");

                HttpURLConnection   conn=(HttpURLConnection) url.openConnection();

                InputStream   in=conn.getInputStream();

                String   path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/a.jpg";

                File   file=new File(path);

                OutputStream   out=new FileOutputStream(file);

                int   totals=conn.getContentLength();

                int   sumCount=0;

                byte   buffer[]=new byte[1024];

                int   len=-1;

                while((len=in.read(buffer))!=-1){

                    out.write(buffer,0,len);

                    sumCount+=len;

                    float   per=sumCount*100f/totals;

                    publishProgress((int)per);

                }

                out.flush();

                out.close();

                in.close();

                conn.disconnect();

             }   catch (Exception e) {

                //   TODO Auto-generated catch block

                e.printStackTrace();

            }

            return   null;

        }

        @Override

        protected   void onProgressUpdate(Integer... values) {

            //   TODO Auto-generated method stub

            super.onProgressUpdate(values);

            pd.setProgress(values[0]);

        }

        @Override

        protected   void onPostExecute(Void result) {

            //   TODO Auto-generated method stub

            super.onPostExecute(result);

            if(pd!=null&&pd.isShowing()){

                pd.dismiss();

            }

             String   path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/a.jpg";

             ImageView   iv=(ImageView) findViewById(R.id.iv);

             Bitmap   bt=BitmapFactory.decodeFile(path);

             iv.setImageBitmap(bt);

        }

    }

 

}</void,integer,void>

 原文链接:http://www.apkbus.com/blog-813041-61158.html

 


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消