riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cn.flightfeather.thirdapp.task;
 
import android.app.Activity;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.bean.entity.Mediafile;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Url;
 
/**
 * Created by note_ff_1602 on 2018/2/9.
 * 下载图片并显示的task
 */
 
public class DownloadAndSetImageTask  {
    private Mediafile mediafile;
    private ImageView imageView;
    private DownloadService downloadService;
    private CommonApplication application;
    private Activity activity;
 
    //下载并显示图片
    public DownloadAndSetImageTask(Mediafile mediafile, ImageView imageView, Activity activity) {
        this.mediafile = mediafile;
        this.imageView = imageView;
        this.activity = activity;
        application = (CommonApplication) activity.getApplicationContext();
    }
    //只下载
    public DownloadAndSetImageTask(Mediafile mediafile, Activity activity) {
        this.mediafile = mediafile;
        application = (CommonApplication) activity.getApplicationContext();
        try {
            this.activity = (Activity) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //执行下载任务
    public void execute(){
        if (imageView!=null){
            imageView.setImageResource(R.drawable.icon_add_photo_waite);
        }
        downloadService = application.getRetrofitImage().create(DownloadService.class);
        String url = mediafile.getExtension1()+mediafile.getGuid()+".jpg";
        Call<ResponseBody> downloadImage = downloadService.downloadImage(url);
        downloadImage.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.body()!=null){
                    File file =  writeResponseBodyToDisk(mediafile,response.body());
                    if (file!=null&&imageView!=null){
                        setImage(file,imageView);
                    }
                }else if (response.errorBody()!=null){
//                    Toast.makeText(application, "获取图片失败", Toast.LENGTH_SHORT).show();
                    Log.e("downloadImage:",response.errorBody().toString());
                }
 
            }
 
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show();
                Log.e("downloadImage:",t.toString());
            }
        });
    }
 
    //将下载的图片写入磁盘
    public  File writeResponseBodyToDisk(Mediafile mediafile, ResponseBody body) {
        if(body==null){
            Toast.makeText(application, "图片源错误", Toast.LENGTH_SHORT).show();
            return null;
        }
        try {
            InputStream is = body.byteStream();
            File file = new File(Environment.getExternalStorageDirectory(),(mediafile.getPath() + mediafile.getDescription()));
            if (file.exists()) {
                file.delete();
                file = new File(Environment.getExternalStorageDirectory(),(mediafile.getPath() + mediafile.getDescription()) );
            }
            file.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fos.close();
            bis.close();
            is.close();
            return file;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //显示图像
    private void setImage(File file,ImageView imageView){
        if (file!=null&&imageView!=null){
            SetImageTask task = new SetImageTask(file,imageView, activity);
            task.execute();
        }
    }
 
    interface DownloadService{
        @GET
        Call<ResponseBody> downloadImage(@Url String fileUrl);
    }
}