簡體   English   中英

Android:可繪制圖標是不可見的

[英]Android: Drawable Icon is invisible

我正在嘗試獲取設備上安裝的應用程序列表,並在 RecyclerView 中顯示它們及其圖標。

以下代碼用於獲取應用程序:

Intent intent = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> installedApps= packageManager.queryIntentActivities(intent, 0);

        installedApps.forEach(p-> {
            String appName = p.loadLabel(packageManager).toString();
            String appPackageName = p.activityInfo.packageName;
            Drawable icon = p.loadIcon(packageManager);
            ApplicationInfo info = p.activityInfo.applicationInfo;

            App_Object appObject = new App_Object(appName, appPackageName, icon, info);
            if(!List_AllApps.contains(appObject)) {
                List_AllApps.add(appObject);
            }
        });
        
List_AllApps.sort(Comparator.comparing(o -> o.app_name));

這里的問題是,一些 Drawable 是不可見的,無法顯示,即使它們不為空。

我發現所說的 Drawable 確實有一個 icon.getIntrinsicHeight() 和一個 icon.getIntrinsicWidth() 為 0,這導致它們不可見。

我已經嘗試將 Drawables 轉換為 ShapeDrawables,因為您可以在那里使用 setIntrinsicWidth() 和 setIntrinsicHeight() 但我沒有成功。 我還嘗試設置未顯示的 Drawables 的邊界,並嘗試將它們轉換為 BitmapDrawables 但沒有任何結果。

這里奇怪的是,有時圖標會在沒有對代碼進行任何更改的情況下顯示,所以我真的很困惑我還能嘗試解決什么問題(getIntrinsicWidth() 和 getIntrinsicHeight() 然后大於 0 並且有所需的值)。

如何解決這個問題? 我可以以任何其他方式調整它們的大小以便它們顯示出來嗎?

這是在 Recyclerview 中為 Imageview 設置 Drawable 時的樣子的圖片Image

任何幫助深表感謝!

要顯示設備上安裝的應用程序列表,您可以使用包管理器的 GET_META_DATA。

 public void getInstalledApplication() { PackageManager pm = getPackageManager(); List < ApplicationInfo > packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); //You have all list of packages in the device. for (ApplicationInfo packageInfo: packages) { //Check if the application is updated or not if ((packageInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { addToInstalledApps(packageInfo, pm); } else if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {//This application is installed in the device system's image } else { addToInstalledApps(packageInfo, pm); } } }

現在您可以獲取設備的 appName 和 appIcon。 下面是方法。

 public void addToInstalledApps(ApplicationInfo packageInfo, PackageManager pm) { int stringId = packageInfo.labelRes; String appName = null; Drawable appIcon = null; try { appName = (String) pm.getApplicationLabel(packageInfo); appIcon = pm.getApplicationIcon(packageInfo); } catch (Exception e) { try { appName = stringId == 0 ? (packageInfo.nonLocalizedLabel != null ? packageInfo.nonLocalizedLabel.toString() : null) : getString(stringId); } catch (Exception ex) { ex.printStackTrace(); } //Exception e.printStackTrace(); } if(pm.getLaunchIntentForPackage(packageInfo.packageName) != null && CommonUtil.isValidString(appName)){ //Now you can design your model and insert data as you want. AppDetailsModel appDetailsModel = new AppDetailsModel(packageInfo.packageName, packageInfo.sourceDir, pm.getLaunchIntentForPackage(packageInfo.packageName), appName, appIcon); appDetailsModelList.add(appDetailsModel); } }

由於其大小為 0,您面臨這個問題。嘗試使用此代碼。 它可能有助於增加尺寸。

 private void checkAllPackageData() { PackageManager packageManager = getActivity().getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> installedApps= packageManager.queryIntentActivities(intent, 0); for (ResolveInfo p : installedApps){ String appName = p.loadLabel(packageManager).toString(); String appPackageName = p.activityInfo.packageName; Drawable icon = p.loadIcon(packageManager); ApplicationInfo info = p.activityInfo.applicationInfo; try { Bitmap bitmap= ((BitmapDrawable) icon).getBitmap(); System.out.println("Before "+ bitmap.getHeight()); Bitmap bitmap1= bitmap.copy(Bitmap.Config.ARGB_8888, true); Bitmap bitmap2 = getResizedBitmap(bitmap1,35,35); System.out.println(bitmap2.getHeight()); } catch (Exception e) { e.printStackTrace(); } } } public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPULATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap( bm, 0, 0, width, height, matrix, false); bm.recycle(); return resizedBitmap; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM