[英]how to extract a png file for the icon of the last installed app
我想知道是否可以从我从手机上下载的最新安装的应用程序中获取图标(PNG形式)?
我正在编写一个可以放在手机上的应用程序,它将提取上次安装的应用程序的程序包名称,通用名称和图标(以png格式)。 我得到了程序包名称和通用名称,但是现在我正在尝试获取图标文件。 这是到目前为止我在网上尚未阅读的有关如何执行此操作的代码,但是我似乎缺少一些东西。 我提取图标的所有代码都在“ 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 ");
}
}
}
Context#openFilesInput()
打开的文件目录与Context#getFilesDir()
返回的私有目录相同。 除非您的手机已植根,否则您将无法通过ADB打开此文件夹。 您可以通过执行以下操作来检索File
对象:
File imageFile = new File(context.getFilesDir(), "imgName.png");
重要的是要注意,当前您是在任何ApplicationInfo#toString()
返回之后命名图像。 您可能要使用applicationName + ".png
打开文件。或者由于这是最新安装的应用程序,因此将其另存为“ latestApp.png”或其他唯一的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.