Java 如何为上次安装的应用程序的图标提取png文件
我想知道是否有可能从我在手机上下载的上次安装的应用程序中获取图标(png格式) 我正在编写一个可以放在手机上的应用程序,它将提取上次安装的应用程序的包名、通用名和图标(png格式)。我得到了包名和通用名,但现在我正试图得到图标文件。这是我在网上读到的关于如何做到这一点的代码,但我似乎遗漏了一些东西。我提取图标的所有代码都在“try”语句中Java 如何为上次安装的应用程序的图标提取png文件,java,android,bitmap,broadcastreceiver,imageicon,Java,Android,Bitmap,Broadcastreceiver,Imageicon,我想知道是否有可能从我在手机上下载的上次安装的应用程序中获取图标(png格式) 我正在编写一个可以放在手机上的应用程序,它将提取上次安装的应用程序的包名、通用名和图标(png格式)。我得到了包名和通用名,但现在我正试图得到图标文件。这是我在网上读到的关于如何做到这一点的代码,但我似乎遗漏了一些东西。我提取图标的所有代码都在“try”语句中 public class NewInstallReceiver extends BroadcastReceiver { @Override public
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 ");
}
}
}
Context#openFilesInput()
打开的文件目录与Context#getFilesDir()
返回的私有目录相同。除非您的手机已根目录,否则您将无法通过ADB打开此文件夹。您可以通过执行以下操作来检索文件
对象:
File imageFile=新文件(context.getFilesDir(),“imgName.png”)代码>
需要注意的是,当前您是根据ApplicationInfo#toString()
返回的内容来命名图像的。您可能希望改为使用applicationName+”.png
打开文件。或者,由于这是最新安装的应用程序,您可以将其另存为“latestApp.png”“或者一些独特的东西 那它似乎不起作用了吗?Hello@DeeV我遇到的问题是,我似乎找不到创建.png的位置?我搜索了我的整个目录,但它似乎不存在我正在创建的文件(applicationName.png)。可能是重复的,非常感谢!这就是我要找的!