【问题标题】:get foreground application icon convert to base64获取前台应用程序图标转换为base64
【发布时间】:2011-09-14 17:45:47
【问题描述】:

我正在尝试获取前台应用程序图标并将其转换为 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);
        }

}

}

提前感谢您的帮助!

【问题讨论】:

  • 随着您发布的代码中的 compress 行被注释掉,outputStream 将是空的——您为什么将其注释掉(它会崩​​溃吗?)另外,println(icon ) 显示?
  • 我把那行注释掉只是想看看我是否可以从程序中得到不同的结果。它不会因未注释而崩溃,但我得到相同的结果。 println(icon) 显示 android.graphics.drawable.BitmapDrawable@40887df0 和 println(bitmapByte) 显示 [B@40893d10

标签: android base64 encode


【解决方案1】:

我稍微修改了您的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);
        }

}

谢谢 迪帕克

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多