How to get the size of installed application?

前端 未结 3 572
名媛妹妹
名媛妹妹 2021-01-15 18:01

I am trying to calculate the size of the installed application.

I found an answer here

I have tested it on some devices, and there is no problem except Sams

3条回答
  •  失恋的感觉
    2021-01-15 18:50

    You can get Size of apps without AIDL Files -------> Kotlin Language

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)  
            
        val list = packageManager.queryIntentActivities(intent,0)
                
        // Set adapter to LIST VIEW
        listView.adapter = getApps(list)
     }
           
     private fun getApps(List: MutableList): List {
                
         val list = ArrayList()
          for (packageInfo in List) {
                          
             val packageName = packageInfo.activityInfo.packageName
               // return size in form of Bytes(Long)             
        val size = File(packageManager.getApplicationInfo(packageName,0).publicSourceDir).length()
              
              val item = AppData(Size)
                list += item
          }
       return list
       }
    }
     // Make Data Class
    data class AppData(val size: Long)
    

    Remember to convert it in MB from Bytes

提交回复
热议问题