package io.github.mayubao.kuaichuan.core.utils; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class ApkUtils { /** * 获取Apk文件的Log图标 * @param context * @param apk_path * @return */ public static Drawable getApkThumbnail(Context context, String apk_path){ if(context == null){ return null; } PackageManager pm = context.getPackageManager(); PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path, PackageManager.GET_ACTIVITIES); ApplicationInfo appInfo = packageInfo.applicationInfo; /**获取apk的图标 */ appInfo.sourceDir = apk_path; appInfo.publicSourceDir = apk_path; if(appInfo != null){ Drawable apk_icon = appInfo.loadIcon(pm); return apk_icon; } return null; } /** * 获取Drawable实际占用大小 * @param drawable * @return */ public static int getDrawableSize(Drawable drawable){ // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); // drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); // Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.fav_jpg); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int len = baos.toByteArray().length; System.out.println("#############>>>>>>>>>" + len); return len; } public static int getBitmapSize(Bitmap bitmap){ /* ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int len = baos.toByteArray().length; System.out.println("#############>>>>>>>>>" + len); return len; */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } // Pre HC-MR1 return bitmap.getRowBytes() * bitmap.getHeight(); } //---------------------------- //1.压缩功能, 压缩到指定大小 //2.Drawable --->>> Bitmap //3.Bitmap --->>> byte[] //4.Bitmap --->>> 生成图片 /** * Drawable转Bitmap * * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable){ if(drawable == null){ return null; } // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; //建立对应的Bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; } /** * Bitmap转ByteArray * * @param bitmap * @return */ public static byte[] bitmapToByteArray(Bitmap bitmap){ if(bitmap == null){ return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } /** * Bitmap 写入到SD卡 * * @param bitmap * @param resPath * @return */ public static boolean bitmapToSDCard(Bitmap bitmap, String resPath){ if(bitmap == null){ return false; } File resFile = new File(resPath); try { FileOutputStream fos = new FileOutputStream(resFile); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * Bitmap压缩到指定的千字节数(比方说图片要压缩成32K,则传32) * * @param srcBitmap * @param maxKByteCount 比方说图片要压缩成32K,则传32 * @return */ public static Bitmap compressBitmap(Bitmap srcBitmap, int maxKByteCount) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); int option = 98; while (baos.toByteArray().length / 1024 >= maxKByteCount && option > 0) { baos.reset(); srcBitmap.compress(Bitmap.CompressFormat.JPEG, option, baos); option -= 2; } } catch (Exception e) { } // bitmap = BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(bais, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; } /** * 压缩图片到指定的文件去————注意,图片尺寸没变,变的只是文件大小(图片的位深度改变了) * * @param srcBitmap * @param maxKByteCount 最大千字节数(比方说图片要压缩成32K,则传32) * @param targetPath 目标图片地址 * @throws IOException */ public static boolean compressBitmap(Bitmap srcBitmap, int maxKByteCount, String targetPath) { boolean result = false; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int option = 98; while (baos.toByteArray().length / 1024 >= maxKByteCount && option > 0) { baos.reset(); srcBitmap.compress(Bitmap.CompressFormat.JPEG, option, baos); option -= 2; } byte[] bitmapByte = baos.toByteArray(); File targetFile = new File(targetPath); if(!targetFile.exists()){ targetFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(targetFile); fos.write(bitmapByte); result = true; try { fos.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } if (!srcBitmap.isRecycled()) { srcBitmap.recycle(); srcBitmap = null; } } catch (Exception e) { e.printStackTrace(); } return result; } //---------------------------- /** * 判断指定报名的App是否装过 * @param context * @param packagename * @return */ public static boolean isAppInstalled(Context context,String packagename) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager().getPackageInfo(packagename, 0); }catch (PackageManager.NameNotFoundException e) { packageInfo = null; e.printStackTrace(); } if(packageInfo ==null){ //System.out.println("没有安装"); return false; }else{ //System.out.println("已经安装"); return true; } } /** * 获取应用包名 * @param context * @param filePath * @return */ public static String getPackageName(Context context, String filePath){ String packageName = ""; if(context == null || TextUtils.isNullOrBlank(filePath)){ return packageName; } PackageManager pm = context.getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES); ApplicationInfo appInfo = null; if (info != null) { appInfo = info.applicationInfo; packageName = appInfo.packageName; } return packageName; } /** * 判断一个Android应用文件是否装过 * @param context * @param filePath * @return true装过, 反之未装过 */ public static boolean isInstalled(Context context, String filePath){ String packageName = getPackageName(context, filePath); return isAppInstalled(context, packageName); } /** * 安装Apk文件 * @param context * @param apkFilePath */ public static void install(Context context, String apkFilePath){ if(context == null){ throw new RuntimeException("ApkUtils install apk method and parameter context == null?"); } File file = new File(apkFilePath); if(!file.exists()){ return ; } Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } }
ApkUtils
最新推荐文章于 2024-09-27 07:16:05 发布