获取前台应用程序图标转换为base64 [英] get foreground application icon convert to base64

查看:610
本文介绍了获取前台应用程序图标转换为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让前台应用程序的图标并将其转换为base64。我能得到前台应用程序的名字,但我不能得到的图标。当我连接code,我得到一个字符串,但它不是图标。我不知道在我的错误。这里是我的code

I'm trying to get the foreground applications icon and convert it to base64. I can get the foreground application's name, but I can't get the icon. When I encode it, I get a string but it is not the icon. I'm not sure where my error is. Here is my code

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, 0, outputStream); 
        byte[] bitmapByte = outputStream.toByteArray();
        bitmapByte = Base64.encode(bitmapByte,Base64.DEFAULT);
        System.out.println(bitmapByte);
        }

}

}

在此先感谢您的帮助!

Thanks in advance for your help!

推荐答案

我已经修改点点你的连接codeIcon()。现在,它的工作的罚款。它具有的实际图像

I have modified little bit your encodeIcon(). Now its working fine. it is having the actual image

public static void encodeIcon(Drawable icon){
        String appIcon64 = new String();
        Drawable ic = icon;

        if(ic !=null){

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
       // bitmap.compress(CompressFormat.PNG, 0, 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);
        }

}

谢谢
迪帕克

Thanks Deepak

这篇关于获取前台应用程序图标转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆