简体   繁体   English

如何为上次安装的应用程序的图标提取png文件

[英]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? 我想知道是否可以从我从手机上下载的最新安装的应用程序中获取图标(PNG形式)?

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). 我正在编写一个可以放在手机上的应用程序,它将提取上次安装的应用程序的程序包名称,通用名称和图标(以png格式)。 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. 我提取图标的所有代码都在“ try”语句中。

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


}



}

The file directory that Context#openFilesInput() opens is the same private directory that Context#getFilesDir() returns. Context#openFilesInput()打开的文件目录与Context#getFilesDir()返回的私有目录相同。 You won't be able to open this folder through ADB unless your phone is rooted. 除非您的手机已植根,否则您将无法通过ADB打开此文件夹。 You can retrieve the File object by doing this though: 您可以通过执行以下操作来检索File对象:

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. 重要的是要注意,当前您是在任何ApplicationInfo#toString()返回之后命名图像。 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. 您可能要使用applicationName + ".png打开文件。或者由于这是最新安装的应用程序,因此将其另存为“ latestApp.png”或其他唯一的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM