Kaynağa Gözat

修复分享图片报错:Software rendering doesn't support hardware bitmaps的导致不兼容8.0以上手机的问题

zengjiebin 7 yıl önce
ebeveyn
işleme
ac1aa19c7d

+ 34 - 0
app/src/main/java/com/sheep/gamegroup/util/GlideImageLoader.java

@@ -39,6 +39,12 @@ public class GlideImageLoader {
                 .load(url)
                 .into(imageView);
     }
+    public static void setImageDisHardConfig(ImageView imageView, String url) {
+        Glide.with(SheepApp.getInstance())
+                .load(url)
+                .apply(new RequestOptions().disallowHardwareConfig())
+                .into(imageView);
+    }
 
     public static void centerImage(ImageView imageView, String url, int id) {
         Glide.with(SheepApp.getInstance())
@@ -135,6 +141,34 @@ public class GlideImageLoader {
                         .into(imageView);
         }
     }
+    /**
+     * 设置二维码图片
+     * @param imageView
+     * @param link
+     * @param w
+     */
+    public static void saveAndSetRqImageDisHardConfig(final ImageView imageView, String link, int w) {
+        String fileName = String.format(Locale.CHINA, "%d.png", link.hashCode());
+        File file = new File(ClassFileHelper.DIR, fileName);
+        if(!file.exists()){
+            Bitmap mBitmap = CodeUtils.createImage(link, w, w, null);
+            if(mBitmap != null) {
+                ViewUtil.saveImage(mBitmap, ClassFileHelper.DIR, fileName);
+                if(imageView != null)
+                    Glide.with(SheepApp.getInstance())
+                            .load(file)
+                            .apply(new RequestOptions().disallowHardwareConfig().override(w, w))
+                            .into(imageView);
+            }
+        }
+        if(file.exists()){
+            if(imageView != null)
+                Glide.with(SheepApp.getInstance())
+                        .load(file)
+                        .apply(new RequestOptions().disallowHardwareConfig().override(w, w))
+                        .into(imageView);
+        }
+    }
 
 
     public static void downloadAndSetImage(final ImageView imageView, final String link, final int w) {

+ 15 - 14
app/src/main/java/com/sheep/gamegroup/util/ViewUtil.java

@@ -10,6 +10,7 @@ import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
@@ -144,10 +145,10 @@ public class ViewUtil {
         View save_pic = view.findViewById(R.id.save_pic);
         ImageView iv_qr_small = view.findViewById(R.id.iv_qr_small);
 
-        GlideImageLoader.saveAndSetRqImage(iv_qr, link, 800);
+        GlideImageLoader.saveAndSetRqImageDisHardConfig(iv_qr, link, 800);
         Glide.with(activity)
                 .load(R.drawable.icon)
-                .apply(new RequestOptions().override(G.WIDTH / 8))
+                .apply(new RequestOptions().disallowHardwareConfig().override(G.WIDTH / 8))
                 .into(iv_qr_small);
 
         dialog_close.setOnClickListener(new View.OnClickListener() {
@@ -669,7 +670,7 @@ public class ViewUtil {
             }
         });
 
-        GlideImageLoader.saveAndSetRqImage(iv_qr, link, 800);
+        GlideImageLoader.saveAndSetRqImageDisHardConfig(iv_qr, link, 800);
         dialog_close.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -831,22 +832,22 @@ public class ViewUtil {
 
     /**
      * 获取一个 View 的缓存视图
+     * 如果报错:Software rendering doesn't support hardware bitmaps
+     * 在Glide 4.3上,hardware bitmaps(开启hardware bitmaps作用大概就是在使用ARGB_8888这种高质量的图片时,图片在内存中只会存在一份,反正就是一种节省内存的机制。) 配置默认是开启的,这也是Glide的一个Bug,高版本不知道修复了没有。
      *
+     * 解决方案是在构建RequestOptions时,将该配置关掉。
+     *
+     * requestOptions.disallowHardwareConfig();
      * @param view
      * @return
      */
     public static Bitmap getCacheBitmapFromView(View view) {
-        final boolean drawingCacheEnabled = true;
-        view.setDrawingCacheEnabled(drawingCacheEnabled);
-        view.buildDrawingCache(drawingCacheEnabled);
-        final Bitmap drawingCache = view.getDrawingCache();
-        Bitmap bitmap;
-        if (drawingCache != null) {
-            bitmap = Bitmap.createBitmap(drawingCache);
-            view.setDrawingCacheEnabled(false);
-        } else {
-            bitmap = null;
-        }
+        int w = view.getWidth();
+        int h = view.getHeight();
+
+        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+        Canvas c = new Canvas(bitmap);
+        view.draw(c);
         return bitmap;
     }
 

+ 6 - 5
app/src/main/java/com/sheep/gamegroup/view/activity/ActInvitation.java

@@ -42,7 +42,6 @@ import com.sheep.gamegroup.util.CommonUtil;
 import com.sheep.gamegroup.util.DataUtil;
 import com.sheep.gamegroup.util.GlideImageLoader;
 import com.sheep.gamegroup.util.ListUtil;
-import com.sheep.gamegroup.util.LogUtil;
 import com.sheep.gamegroup.util.StringUtils;
 import com.sheep.gamegroup.util.ViewHolder;
 import com.sheep.gamegroup.util.ViewUtil;
@@ -255,7 +254,7 @@ public class ActInvitation extends BaseActivity {
                     Glide.with(SheepApp.getInstance())
                             .load(file)
                             .listener(listener)
-                            .apply(new RequestOptions().override(w, h).transform(new RoundedCorners(getResources().getDimensionPixelSize(R.dimen.content_padding_8))))
+                            .apply(new RequestOptions().disallowHardwareConfig().override(w, h).transform(new RoundedCorners(getResources().getDimensionPixelSize(R.dimen.content_padding_8))))
                             .into(image_full);
                     updateLayoutParams(index, 500L);
                 }
@@ -333,7 +332,7 @@ public class ActInvitation extends BaseActivity {
                 float radius = 4 / 216F;
                 float topRadius = 2 / 216F;
                 float logoRadius = rqRadius * 16 / 68;
-                GlideImageLoader.saveAndSetRqImage(image_bottom, item.getLink(), (int) (imgWidth * rqRadius));
+                GlideImageLoader.saveAndSetRqImageDisHardConfig(image_bottom, item.getLink(), (int) (imgWidth * rqRadius));
                 //重新设置logo的宽高
                 ViewGroup.LayoutParams layoutParams = image_logo.getLayoutParams();
                 layoutParams.width = (int) (imgWidth * logoRadius);
@@ -400,7 +399,7 @@ public class ActInvitation extends BaseActivity {
                 ImageView ask_to_share_item_img = itemView.findViewById(R.id.ask_to_share_item_img);
                 TextView ask_to_share_item_name = itemView.findViewById(R.id.ask_to_share_item_name);
                 final int position = holder.getAdapterPosition();
-                GlideImageLoader.setImage(ask_to_share_item_img, getNetImgByName(imgList.get(position)));
+                GlideImageLoader.setImageDisHardConfig(ask_to_share_item_img, getNetImgByName(imgList.get(position)));
                 ask_to_share_item_name.setText(nameList.get(position));
                 itemView.setOnClickListener(new View.OnClickListener() {
                     @Override
@@ -412,13 +411,15 @@ public class ActInvitation extends BaseActivity {
                                 PictureInvitationEntity picItem = ListUtil.getItem(mPictureList, curPosition);
                                 if (picItem != null && picItem.isLoaded()) {
                                     try {
-                                        String fileName = String.format(Locale.CHINA, "share%d%s%d.jpg", position, BuildConfig.FLAVOR, picItem.getPicture().hashCode());
+                                        String fileName = String.format(Locale.CHINA, "share%d%s%d.jpg", curPosition, BuildConfig.FLAVOR, picItem.getPicture().hashCode());
                                         file = new File(ClassFileHelper.DIR, fileName);
                                         if (!file.exists()) {
                                             file = ViewUtil.saveImage(viewList.get(curPosition), ClassFileHelper.DIR, fileName);
                                         }
                                     } catch (Exception e) {
                                         e.printStackTrace();
+                                        G.showToast(e.getMessage());
+                                        return;
                                     }
                                 } else {
                                     G.showToast("图片正在加载中,请稍等");