修复在小内存设备上加载本地应用图标会内存不够然后闪退的问题

This commit is contained in:
chenjuntao
2019-04-26 16:18:44 +08:00
parent 193831ae7e
commit b78fa40eff
8 changed files with 28 additions and 10 deletions

View File

@ -7,6 +7,9 @@ import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.os.Build;
import com.halo.assistant.HaloApp;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
@ -186,15 +189,30 @@ public class BitmapUtils {
/**
* Drawable转Bitmap
* @param isSquare 是否是正方形
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
public static Bitmap drawableToBitmap(Drawable drawable, boolean isSquare) {
if (drawable == null) {
return null;
}
int w,h;
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
w = drawable.getIntrinsicWidth();
h = drawable.getIntrinsicHeight();
// 在低于 5.1 和运行内存小于 2G 的设备上减小图片大小,避免 OOM128 * 128 又不是不能看
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
|| DeviceUtils.getTotalRamSizeOfDevice(HaloApp.getInstance().getApplication()) < 2000) {
if (isSquare) {
w = w > 128 ? 128 : w;
h = h > 128 ? 128 : h;
} else {
w = w > 128 ? w / 2 : w;
h = h > 128 ? h / 2 : h;
}
}
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;

View File

@ -130,7 +130,7 @@ public class DialogUtils {
ApplicationInfo appInfo = info.applicationInfo;
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
Bitmap bitmap = BitmapUtils.drawableToBitmap(appInfo.loadIcon(pm));
Bitmap bitmap = BitmapUtils.drawableToBitmap(appInfo.loadIcon(pm), true);
ImageView imageView = new ImageView(activity);
imageView.setLayoutParams(new LinearLayout.LayoutParams(DisplayUtils.dip2px(activity, 25)

View File

@ -114,7 +114,7 @@ public class CleanApkAdapter extends BaseRecyclerAdapter<KcSelectGameViewHolder>
appInfo.sourceDir = apk_path;
appInfo.publicSourceDir = apk_path;
Drawable apk_icon = appInfo.loadIcon(pm);
apkEntity.setGameBm(BitmapUtils.drawableToBitmap(apk_icon));
apkEntity.setGameBm(BitmapUtils.drawableToBitmap(apk_icon, true));
/** apk的绝对路劲 */
apkEntity.setGamePath(apk_path);
/** apk的版本名称 String */

View File

@ -407,7 +407,7 @@ public class InstallFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
File file = new File(installedPackage.applicationInfo.sourceDir);
fileInfo.setSize(file.length());
Drawable drawable = installedPackage.applicationInfo.loadIcon(pm);
fileInfo.setBitmap(BitmapUtils.drawableToBitmap(drawable));
fileInfo.setBitmap(BitmapUtils.drawableToBitmap(drawable, true));
fileInfo.setFileTag(String.valueOf(System.currentTimeMillis()));
}
}

View File

@ -104,7 +104,7 @@ public class KcSelectGameAdapter extends BaseRecyclerAdapter<ViewHolder> {
installGameEntity.setGamePath(installedPackage.applicationInfo.sourceDir);
try {
Drawable drawable = installedPackage.applicationInfo.loadIcon(pm);
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable));
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable, true));
installGameEntity.setGameVersion(installedPackage.versionName);
installGameEntity.setGameName(installedPackage.applicationInfo.loadLabel(pm).toString());

View File

@ -136,7 +136,7 @@ class GameDownloadFragmentAdapter extends BaseRecyclerAdapter<ViewHolder> {
ApplicationInfo appInfo = info.applicationInfo;
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
Bitmap bitmap = BitmapUtils.drawableToBitmap(appInfo.loadIcon(pm));
Bitmap bitmap = BitmapUtils.drawableToBitmap(appInfo.loadIcon(pm), true);
viewHolder.dmIcon.setImageBitmap(bitmap);
}
} catch (JSONException e) {

View File

@ -110,7 +110,7 @@ public class SelectGameDialogAdapter extends BaseRecyclerAdapter<VotingSelectGam
installGameEntity.setGamePath(installedPackage.applicationInfo.sourceDir);
try {
Drawable drawable = installedPackage.applicationInfo.loadIcon(pm);
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable));
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable, true));
installGameEntity.setGameVersion(installedPackage.versionName);
installGameEntity.setGameName(installedPackage.applicationInfo.loadLabel(pm).toString());

View File

@ -62,7 +62,7 @@ public class SuggestSelectGameAdapter extends BaseRecyclerAdapter<SelectGameView
installGameEntity.setGamePath(installedPackage.applicationInfo.sourceDir);
try {
Drawable drawable = installedPackage.applicationInfo.loadIcon(pm);
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable));
installGameEntity.setGameBm(BitmapUtils.drawableToBitmap(drawable, true));
installGameEntity.setGameVersion(installedPackage.versionName);
installGameEntity.setGameName(installedPackage.applicationInfo.loadLabel(pm).toString());