码迷,mamicode.com
首页 > 移动开发 > 详细

获取应用图标并转为bitmap适配android10

时间:2020-01-10 00:26:56      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:eth   自己的   conf   exception   class   ret   gem   dir   raw   

 /**
     * 获取图标 bitmap
     *
     * @param context
     * Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)//android 10存储图片地址
     */
    public static synchronized Bitmap getBitmap(Context context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getApplicationContext()
                    .getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(
                    context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        Drawable d = packageManager.getApplicationIcon(applicationInfo); //xxx根据自己的情况获取drawable

        Bitmap APKicon;
        if(d instanceof BitmapDrawable) {
            APKicon  = ((BitmapDrawable)d).getBitmap();
        }else{
            Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(),d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            d.draw(canvas);
            APKicon = bitmap;
        }
//        BitmapDrawable bd = (BitmapDrawable) d;
//        Bitmap bm = bd.getBitmap();
        return APKicon;
    }

  

获取应用图标并转为bitmap适配android10

标签:eth   自己的   conf   exception   class   ret   gem   dir   raw   

原文地址:https://www.cnblogs.com/wang-jingyuan/p/12173978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!