How to get the size of installed application?

前端 未结 3 568
名媛妹妹
名媛妹妹 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:31

    try this...

    public static long getApkSize(Context context, String packageName)
            throws NameNotFoundException {
        return new File(context.getPackageManager().getApplicationInfo(
                packageName, 0).publicSourceDir).length();
    }
    
    0 讨论(0)
  • 2021-01-15 18:48

    Maybe this link will help you:

    http://nsamteladze.blogspot.com/2012/10/get-apks-code-size-in-android.html

    Basically he is making use of the concept of reflection, which is a powerful tool but not always advisable. Read more about it here :What is reflection and why is it useful? and if it is suitable for you, make use of it.

    0 讨论(0)
  • 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<ResolveInfo>): List<AppData> {
                
         val list = ArrayList<AppData>()
          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

    0 讨论(0)
提交回复
热议问题