From 0e870a0d919a28a45084a7df5f662afbfd7e086d Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期二, 15 十二月 2020 14:44:28 +0800
Subject: [PATCH] DownloadAndSetImageTask和SetImageTask两个类由于在子线程执行,会经常出现activity关闭后继续执行导致报错崩溃问题,修复此bug

---
 app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java |   85 +++++++-----------------------------------
 1 files changed, 14 insertions(+), 71 deletions(-)

diff --git a/app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java b/app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java
index 210a54a..8232a73 100644
--- a/app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java
+++ b/app/src/main/java/cn/flightfeather/thirdapp/task/SetImageTask.java
@@ -1,98 +1,41 @@
 package cn.flightfeather.thirdapp.task;
 
 import android.app.Activity;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.graphics.Matrix;
-import android.os.AsyncTask;
-import android.os.Build;
-import android.support.annotation.RequiresApi;
 import android.widget.ImageView;
+
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.util.Util;
 
 import java.io.File;
 
 import cn.flightfeather.thirdapp.R;
-import cn.flightfeather.thirdapp.module.inspectioninfo.ProblemChangeDetailActivity;
 
 /**
  * Created by note_ff_1602 on 2017/10/26.
  * 璁剧疆鍥剧墖鐨則ask
+ *
  */
 
-public class SetImageTask extends AsyncTask {
+// FIXME: 2020/11/12 鍘熸潵鐨勫疄鐜版柟寮忓彲鑳戒細瀵艰嚧oom锛屽洜姝や慨鏀逛负浣跨敤Glide鏉ュ疄鐜板浘鐗囩殑鏄剧ず銆�
+public class SetImageTask {
     private File file;
     private ImageView miv_photo;
-    private ProblemChangeDetailActivity activity;
+    private Activity activity;
 
     public SetImageTask(File file, ImageView miv_photo, Activity activity) {
-        super();
         this.file = file;
         this.miv_photo = miv_photo;
-        try {
-            this.activity = (ProblemChangeDetailActivity) activity;
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public SetImageTask(File file, ImageView miv_photo) {
-        super();
-        this.file = file;
-        this.miv_photo = miv_photo;
-    }
-
-    //鍘嬬缉鍓嶅厛鎶婂浘鐗囪缃负绛夊緟
-    @Override
-    protected void onPreExecute() {
-        super.onPreExecute();
-        miv_photo.setImageResource(R.drawable.icon_add_photo_waite);
-    }
-
-    //鍚庡彴鍘嬬缉鍥剧墖
-    @Override
-    protected Object doInBackground(Object[] params) {
-        BitmapFactory.Options options = new BitmapFactory.Options();
-        options.inPreferredConfig = Bitmap.Config.RGB_565;
-        Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
-        if (bm == null) return null;
-        //鍘嬬缉缂╃暐鍥�
-        Matrix matrix = new Matrix();
-        matrix.setScale(0.12f, 0.12f);
-        //鍘嬬缉缂╃暐鍥�
-        Bitmap bmSmall = null;
-        int normalSize = 100;
-        int width,height =0;
-        if (bm.getWidth()>=bm.getHeight()){
-            width = normalSize;
-            height = normalSize*bm.getHeight()/bm.getWidth();
-        }else {
-            height =normalSize;
-            width = normalSize*bm.getWidth()/bm.getHeight();
-        }
-
-        if (bm!=null){
-            bmSmall= Bitmap.createScaledBitmap(bm,width,height,true);
-//            bmSmall= Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
-        }
-
-
-        return bmSmall;
+        this.activity = activity;
     }
 
 
-
-    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
-    @Override
-    protected void onPostExecute(Object o) {
-        super.onPostExecute(o);
-        Bitmap bmSmall = (Bitmap) o;
-        if (bmSmall!=null){
-            miv_photo.setImageBitmap(bmSmall);
-//            if (activity != null) {
-//                activity.postponeEnterTransition();
-//            }
+    public void execute() {
+        if (Util.isOnMainThread() && activity != null && !activity.isDestroyed()) {
+            Glide.with(miv_photo)
+                    .load(file)
+                    .placeholder(R.drawable.icon_add_photo_waite)
+                    .into(miv_photo);
         }
-
     }
 
 

--
Gitblit v1.9.3