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 downloadImage = downloadService.downloadImage(url); downloadImage.enqueue(new Callback() { @Override public void onResponse(Call call, Response 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 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 downloadImage(@Url String fileUrl); } }