Search code examples
javaandroidbitmapbroadcastreceiverimageicon

how to extract a png file for the icon of the last installed app


I was wondering if it is possible to get the icon (in png form) from the last installed app that I download on my phone?

I am writing an app that I can put on my phone, which will extract the last installed app's package name, common name, and icon (in png form). I got the package name and common name, but now I am trying to get the icon file. Here is the code I have so far from reading around on the web for how to do this, but I seem to be missing something. All of my code for extracting the icon is in the "try" statement.

public class NewInstallReceiver extends BroadcastReceiver
{


@Override
public void onReceive(Context context, Intent intent) {

    Log.d("NewInstallReceiver", "Intent: " + intent.getAction());


    final PackageManager pm = context.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo( intent.getData().getSchemeSpecificPart(), 0);
        Log.d("PACKAGE NAME","Intent" + ai);
    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    Log.d("Application NAME", "Intent: " + applicationName);




    // http://www.carbonrider.com/2016/01/01/extract-app-icon-in-android/

    try {
        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());
    }


    catch (Exception e){
        Log.d("CATCH", "CATCH ");
    }


}



}

Solution

  • The file directory that Context#openFilesInput() opens is the same private directory that Context#getFilesDir() returns. You won't be able to open this folder through ADB unless your phone is rooted. You can retrieve the File object by doing this though:

    File imageFile = new File(context.getFilesDir(), "imgName.png");

    It's important to note, that currently you're naming the image after whatever ApplicationInfo#toString() returns. You may want to open the file with the applicationName + ".png instead. Or since this is the latest installed app, you save it as "latestApp.png" or something unique.