微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取前台应用程序图标转换为base64

如何解决获取前台应用程序图标转换为base64

| 我正在尝试获取前台应用程序图标并将其转换为base64。我可以获取前台应用程序的名称,但是无法获取图标。编码时,我得到一个字符串,但不是图标。我不确定我的错误在哪里。这是我的代码
public class RunningServices {
    private static Context context;
    private static String ACTIVITY_SERVICE = \"activity\";

    public RunningServices(Context myContext){
        context = myContext;
    }
    public static RunningAppProcessInfo getRunningServices(){
        RunningAppProcessInfo result = null,info = null;
        String currentApplication;
        ActivityManager am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
        Drawable icon = null;

        PackageManager pm = context.getPackageManager();
        List <RunningAppProcessInfo> l = am.getRunningAppProcesses();
        Iterator <RunningAppProcessInfo> i = l.iterator();
        while(i.hasNext()){
            info = i.next();
            if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                try {
                    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_Meta_DATA));
                    currentApplication= c.toString();
                    icon = pm.getApplicationIcon(pm.getApplicationInfo(info.processName,PackageManager.GET_Meta_DATA));
                    System.out.println(currentApplication);
                    System.out.println(icon);

                } catch (NameNotFoundException e) {
                    // Todo Auto-generated catch block
                    e.printstacktrace();
                }
                break;
        }
        }
        encodeIcon(icon);
        return result;
}
    public static void encodeIcon(Drawable icon){
        String appIcon64 = new String();
        Drawable ic = icon;

        if(ic !=null){
        Bitmap bitmap = ((BitmapDrawable)ic).getBitmap();                
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
       // bitmap.compress(CompressFormat.PNG,outputStream); 
        byte[] bitmapByte = outputStream.toByteArray();
        bitmapByte = Base64.encode(bitmapByte,Base64.DEFAULT);
        System.out.println(bitmapByte);
        }

}

}
在此先感谢您的帮助!     

解决方法

        我已经稍微修改了您的ѭ1modified。现在其工作正常。它有实际的形象
public static void encodeIcon(Drawable icon){
        String appIcon64 = new String();
        Drawable ic = icon;

        if(ic !=null){

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
       // bitmap.compress(CompressFormat.PNG,outputStream); 

        BitmapDrawable bitDw = ((BitmapDrawable) ic);
        Bitmap bitmap = bitDw.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
        byte[] bitmapByte = stream.toByteArray();


        bitmapByte = Base64.encode(bitmapByte,Base64.DEFAULT);
        System.out.println(\"..length of image...\"+bitmapByte.length);
        }

}
谢谢 迪帕克     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。