java - Get Android Installed App Icon in Unity -


i'm writing quite difficult (for me) issue. quickly, want installed apps in device (icon , label). i'm using simple code works getting name of app. use android java class these informations. problem android optains app icon "drawable". unity cannot read , display sprite. wondering if there way sort issue out. have tried encode drawable base64 string unity "replies" me "invalid string length" error, maybe "infinite" length of base64 string. trying convert drawable byte array , use create texture texture.loadimage(byte[]) doesn't work. here code:

 androidjavaclass jc = new androidjavaclass("com.unity3d.player.unityplayer");     androidjavaobject currentactivity = jc.getstatic<androidjavaobject>("currentactivity");     int flag = new androidjavaclass("android.content.pm.packagemanager").getstatic<int>("get_meta_data");     androidjavaobject pm = currentactivity.call<androidjavaobject>("getpackagemanager");     androidjavaobject packages = pm.call<androidjavaobject>("getinstalledapplications", flag);      int count = packages.call<int>("size");     string[] names = new string[count];     int ii =0;     for(int i=0; ii<count;){             //get object         androidjavaobject currentobject = packages.call<androidjavaobject>("get", ii );         try{                 //try add variables next entry             names[i] = pm.call<string>("getapplicationlabel", currentobject);             androidjavaobject icon = pm.call<androidjavaobject>("getapplicationicon", currentobject);//this part java (android studio) code using android java object , class of unity. maybe error here             androidjavaobject bitmap = icon.call<androidjavaobject>("getbitmap");             androidjavaclass stream = new androidjavaclass("java.io.bytearrayoutputstream");             bitmap.call("compress",(new androidjavaclass("java.io.bytearrayoutputstream")).call<androidjavaobject>("jpeg"), 100, stream);             byte[] bitmapdata = stream.call<byte[]>("tobytearray");//to here             texture2d mytexture = new texture2d(50, 50); // no idea default size be?? important??             if (!mytexture.loadimage(bitmapdata)) {                 debug.log("failed loading image data!");             }             else {                 debug.log("loadimage - still sane here - size: " + mytexture.width + "x" + mytexture.height);                 gameobject app = (gameobject)instantiate(app, vector3.zero, quaternion.identity);                 app.getcomponent<rawimage>().texture = mytexture;//here code should display icon texture (sprite best)             }             i++;             ii++;         }         catch(exception e){             debug.logerror(e,this);                 //if fails, go next app , try add same entry.             ii++;         }      } 

here there java android studio working code:

        bitmap bitmap = ((bitmapdrawable) item.geticon()).getbitmap(); //item.geticon() returns drawable correctly         bytearrayoutputstream baos = new bytearrayoutputstream();         bitmap.compress(bitmap.compressformat.png, 100, baos);         byte[] bytearray = baos.tobytearray(); 

thanks help.

i have created android jar plugin on android studio (no activity) , have imported in unity3d (androidmanifest.xml , classes.jar in assets>plugins>android>libs). inside plugin have added class , void byte[] of icon. java android plugin class:

public class pluginclass {  public static byte[] geticon(packagemanager pm, applicationinfo applicationinfo) {     try {         bitmapdrawable icon = (bitmapdrawable) pm.getapplicationicon(applicationinfo);         bitmap bmp = icon.getbitmap();         bytearrayoutputstream stream = new bytearrayoutputstream();         bmp.compress(bitmap.compressformat.png, 100, stream);         byte[] bytearray = stream.tobytearray();         return bytearray;     } catch (exception e) {         return null;     } }  public static boolean issystem(applicationinfo applicationinfo){     return (applicationinfo.flags & applicationinfo.flag_system ) != 0;    } } have invoked in unity c# script:  void start () {     androidjavaclass jc = new androidjavaclass("com.unity3d.player.unityplayer");     androidjavaobject currentactivity = jc.getstatic<androidjavaobject>("currentactivity");     int flag = new androidjavaclass("android.content.pm.packagemanager").getstatic<int>("get_meta_data");     androidjavaobject pm = currentactivity.call<androidjavaobject>("getpackagemanager");     androidjavaobject packages = pm.call<androidjavaobject>("getinstalledapplications", 0);     int count = packages.call<int>("size");     string[] names = new string[count];     list<byte[]> byteimg = new list<byte[]>();     int ii =0;     for(int i=0; ii<count;){         androidjavaobject currentobject = packages.call<androidjavaobject>("get", ii );         try{             names[i] = pm.call<string>("getapplicationlabel", currentobject);             var plugin = new androidjavaclass("com.mypackagename.pluginclass");             if(plugin.callstatic<bool>("issystem",currentobject)){                 ii++;                 continue;             }             byte[] decodedbytes = plugin.callstatic<byte[]>("geticon", pm, currentobject);             texture2d text = new texture2d(1, 1, textureformat.argb32, false);             text.loadimage(decodedbytes);             sprite sprite = sprite.create (text, new rect(0,0,text.width,text.height), new vector2(.5f,.5f));             i++;             ii++;         }         catch(exception e){             debug.logerror(e,this);             ii++;         }      } 

thanks help


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -