Extract app icon in Android

Android has been on my radar for a long time, but I could never proceed with simple hello world program and few basic stuffs. From last couple of months, I have been working with both Hybrid and Native application development for Android. The experience has been enriching as I have explored lot of things in Android App Development. There are still few areas (testing, automation, profiling, game development etc.) that yet to be discovered. While developing one of the functionality, I came across a strange problem. The need was to extract an icon of any installed app and store it. Later the extracted icons must be displayed in GridView for user actions. I started using following code, to extract and save the icon

Drawable appIcon = packageManager.getApplicationIcon("");
BitmapDrawable bitmapIcon = (BitmapDrawable)appIcon;
FileOutputStream fosIcon = context.openFileOutput(packageName + ".png", Context.MODE_PRIVATE);
bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
//flush stream... close stream...

And to get the drawable image from serialized state, I used following code

inputStream = context.openFileInput(packageName + ".png");
drawable = Drawable.createFromStream(inputStream, null);
//close input stream...

Surprizingly, the results I got was bit weird and the image was getting scaled down. The original size of the image was 144px X 144px. After reading it from the stream and converting it into Drawable, the size was reduced to 48px X 48px. I tried setting bounds for the Drawable using


But that didn’t help and the image read from serialized state was still smaller. There was no clue as whether there is problem while serializing image or while reading it. After spending hours, figuring out what went wrong, I came across another approach which can be used to read images from serialized state. I change the code to read from Serialized image as

inputStream = context.openFileInput(packageName + ".png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
drawable = new BitmapDrawable(context.getResources(), bitmap);

and it worked. I tried searching for the reasons behind such API behavior but couldn’t locate one except – stackoverflow.

By Carbon Rider

Hi this is Yogesh, welcome to my world. Being passionate about learning new technologies and building frameworks, I end up spending most of my time in front of computer. But over last few months, I realised apart from being good coder and designer (Yes I am) I have something hidden in me. And thats called ART.

2 replies on “Extract app icon in Android”

Hello, I am writing a program to get the app icon of the last app that I installed on my phone. I would like the icon that I am getting to be in png format. I have used your code for “EXTRACT APP ICON IN ANDROID”. Here is my code with print statements after each section:

Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d(“ICON BITMAPDRAWABLE”, “Intent: ” + icon);

BitmapDrawable bitmapIcon = (BitmapDrawable)icon;
Log.d(“ICON 11111”, “Intent: ” + bitmapIcon);

FileOutputStream fosIcon = context.openFileOutput(ai + “.png”, Context.MODE_PRIVATE);
Log.d(“ICON 22222”, “Intent: ” + fosIcon);

bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
InputStream inputStream = context.openFileInput(ai + “.png”);
Log.d(“ICON NAME 33333”, “Intent: ” + inputStream);

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Log.d(“ICON bitmap 44444”, “Intent: ” + bitmap);

Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
Log.d(“ICON drawable 55555”, “Intent: ” + drawable);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

MY OUTPUTS from the print statements are BITMAPs for the icon. I would like to be able to get a png file of the icon. Is that possible? How are you using this to create an image, and where is that image created?

Hi Natali,

Sorry for my delay in reply. You are already creating your files in PNG format. Still, few things, I would like to clarify are
When you store the extracted icon to the disk, using
bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
I will suggest you to check “files” folder created under /data/data/your_app_id/files
The shell command to check above location is

adb shell "run-as your_app_id ls /data/data/your_app_id/files"

You should see the icon you have copied should be present in that folder. You can pull these files using adb commands to your desktop/laptop and verify the contents. To render the graphics files, you must convert them to BitmapDrawable.

Now to answer your 2nd query, I am using the app icons to display a grid like structure (similar to android apps listing). Feel free, if you have any queries.

Leave a Reply