2

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!

  • With the compress line commented out as in the code you posted, the outputStream is going to be empty-- why did you comment that out (was it crashing?) Also, what does the println(icon) show? – antlersoft Jun 14 '11 at 14:08
  • I commented that line out just to see if I could get a different result out of the program. It does not crash with it uncommented, but I get the same result. And the println(icon) shows android.graphics.drawable.BitmapDrawable@40887df0 and the println(bitmapByte) shows [B@40893d10 –  Jun 14 '11 at 14:14

1 Answers1

1

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

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243