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
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