继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

开满天机
关注TA
已关注
手记 94
粉丝 74
获赞 262

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

 


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP