如何获得已安装的应用程序的大小?[英] How to get the size of installed application?

本文是小编为大家收集整理的关于如何获得已安装的应用程序的大小?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试计算安装应用程序的大小.

我找到答案在这里

我已经在某些设备上进行了测试,除了三星Galaxy Note3(4.3).

,没有问题.

我得到此错误:java.lang.nosuchmethodexception:getPackagesizeinfo()

我想知道还有其他方法可以获取安装应用程序的大小吗?

推荐答案

尝试这个...

public static long getApkSize(Context context, String packageName)
        throws NameNotFoundException {
    return new File(context.getPackageManager().getApplicationInfo(
            packageName, 0).publicSourceDir).length();
}

其他推荐答案

也许此链接会帮助您:

.

基本上,他正在利用反射的概念,这是一个强大的工具,但并非总是建议.在此处阅读更多有关它的信息:什么是反射,为什么有用?/a>,如果适合您,请使用它.

其他推荐答案

您可以在没有aidl文件的情况下获得大小的应用程序 -------> kotlin语言

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)

记住从字节中将其转换为从字节中转换为

本文地址:https://www.itbaoku.cn/post/1937909.html

问题描述

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 Samsung Galaxy Note3(4.3).

I get this error: java.lang.NoSuchMethodException: getPackageSizeInfo()

I'm wondering is there any other way to get the size of an installed app?

推荐答案

try this...

public static long getApkSize(Context context, String packageName)
        throws NameNotFoundException {
    return new File(context.getPackageManager().getApplicationInfo(
            packageName, 0).publicSourceDir).length();
}

其他推荐答案

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.

其他推荐答案

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