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

发布于 2024-11-15 03:48:03 字数 2194 浏览 3 评论 0原文

我正在尝试获取前台应用程序图标并将其转换为 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, 0, outputStream); 
        byte[] bitmapByte = outputStream.toByteArray();
        bitmapByte = Base64.encode(bitmapByte,Base64.DEFAULT);
        System.out.println(bitmapByte);
        }

}

}

提前感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2024-11-22 03:48:03

我对您的 encodeIcon() 进行了一些修改。现在它工作正常。它有实际图像

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);
        }

}

谢谢
迪帕克

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文