Demand:
Place all app icons in your phone with the app name, suffix jpg, and store in SD card
Ideas:
1. Get information about installed packages by Getpackagemanager (). Getinstalledpackages (0) PackageInfo
2. Package information PackageInfo Get the package name, version name, version number, and of course the app icon drawable object
3. Convert the Drawable object to a Bitmap object and then save it (...) This is the only way to do it now, and I don't know how to deal with drawable.
Code:
1 Packagecom.example.utils;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 Importjava.util.List;6 7 Importandroid.app.Activity;8 ImportAndroid.content.pm.PackageInfo;9 ImportAndroid.graphics.Bitmap;Ten ImportAndroid.graphics.Canvas; One ImportAndroid.graphics.PixelFormat; A Importandroid.graphics.drawable.Drawable; - ImportAndroid.os.Bundle; - the Public classMainactivityextendsActivity { - - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + A //Get PackageInfo atlist<packageinfo> packages = Getpackagemanager (). Getinstalledpackages (0); - - //get the information you need and store it (you can use the enhanced for loop, of course) - for(inti = 0; I < packages.size (); i++) { -PackageInfo PackageInfo =Packages.get (i); -String AppName =PackageInfo.applicationInfo.loadLabel (Getpackagemanager ()). ToString (); indrawable AppIcon =PackageInfo.applicationInfo.loadIcon (Getpackagemanager ()); - to //turn drawable into a bitmap object +Bitmap BM =Drawabletobitmap (AppIcon); - the //Specify the storage path and store file name format *File dir =NewFile ("/sdcard/pictures/"); $File outputimg =NewFile (dir, AppName + ". jpg");Panax Notoginseng - //Bitmap Stored Procedures the Try { +FileOutputStream out =NewFileOutputStream (outputimg); ABm.compress (Bitmap.CompressFormat.PNG, 90, out); the Out.flush (); + out.close (); -LOGUTIL.E ("Test", "saved" +i); $}Catch(Exception e) { $ //don't handle exceptions. - } - the } - }Wuyi the /** - * Turn drawable into bitmap Wu * @paramdrawable - * @return About */ $ PublicBitmap Drawabletobitmap (drawable drawable) { - -Bitmap Bitmap =Bitmap.createbitmap ( - A drawable.getintrinsicwidth (), + the drawable.getintrinsicheight (), - $Drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_8888 the the : Bitmap.Config.RGB_565); the theCanvas Canvas =NewCanvas (bitmap); - in //Canvas.setbitmap (bitmap); the theDrawable.setbounds (0, 0, Drawable.getintrinsicwidth (), About drawable.getintrinsicheight ()); the the Drawable.draw (canvas); the + returnbitmap; - the }Bayi}
Note You need to declare permissions :
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Reference: http://www.cnblogs.com/mainroadlee/archive/2011/05/23/android_get_installed_app.html
android--Get mobile app icon