Get Application Directory - android

Does someone know how do I get the path of my application directory? (e.g. /data/data/my.app.lication/)
Currently I'm using this method: myActivity.getFilesDir().getParent(); but it seems to me like a workaround when there's a simpler solution. Also, the side-effect is the creation of the files directory, which is un-needed.
Clarification: First - Thanks for the repliers. I try to understand if there's already exists method that does it, not for another work-around.

There is a simpler way to get the application data directory with min API 4+. From any Context (e.g. Activity, Application):
getApplicationInfo().dataDir
http://developer.android.com/reference/android/content/Context.html#getApplicationInfo()

PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
If eclipse worries about an uncaught NameNotFoundException, you can use:
PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}

Just use this in your code
context.getApplicationInfo().dataDir

I got this
String appPath = App.getApp().getApplicationContext().getFilesDir().getAbsolutePath();
from here:

For current Android application package:
public String getDataDir(Context context) throws Exception {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
}
For any package:
public String getAnyDataDir(Context context, String packageName) throws Exception {
return context.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.dataDir;
}

If you're trying to get access to a file, try the openFileOutput() and openFileInput() methods as described here. They automatically open input/output streams to the specified file in internal memory. This allows you to bypass the directory and File objects altogether which is a pretty clean solution.

Based on #jared-burrows' solution. For any package, but passing Context as parameter...
public static String getDataDir(Context context) throws Exception {
return context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.applicationInfo.dataDir;
}

Related

How to get Custom SW version number of android

I have a Marshmallow device. It has custom built SW.
I have seen the build.prop file. There I can perceive the Software version name comes from ro.custom.build.version
My question is - how can I get the "ro.custom.build.version" information programmatically in my application ?
I got the answer. It is very simple -
First, I created a function to read from SystemProperties.
public String getSystemProperty(String key) {
String value = null;
try {
value = (String) Class.forName("android.os.SystemProperties")
.getMethod("get", String.class).invoke(null, key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
And then called the function with "ro.custom.build.version" as a key
getSystemProperty("ro.custom.build.version");
Special thanks to #sasikumar for giving me the hint
You have to do as below :
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;
To get the Version code, Use as below :
int verCode = pInfo.versionCode;
Check android.os.Build.VERSION. You can use INCREMENTAL.

How to get package name from apk in Android?

I am developing an app which will contain a list of Apps. On click the user will be redirected to the Play Store to download this app. On successful download I have to send that apps package name to a server to validate it. How can I do that?
I assume you want to do this at runtime, so your app can read its own package_id w/o having this hardcoded. For that you need to use PackageManager's getPackageInfo() method:
protected String getPackageName() {
try {
PackageInfo packageInfo = getPackageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.applicationInfo.packageName;
} catch (Exception e) {
e.printStacktrace();
}
return null;
}
Use apkanalyzer, its part of the Android studio:
apkanalyzer manifest application-id path/to/apk/file.apk

How can i get the apk file name and path programmatically?

I want to get the exact file name of a program if I already know the package name of the target apk. For instance, if I know the package name of my apk, which is com.packagename, how can I get the exact path and file name of that package? Btw, i don't want to get just MY apk location, i want the location of any package name i apply. SystemTuner pro is able to do this so i know it is possible, just not sure how.
Thanks guys!
/**
* Get the apk path of this application.
* #param context any context (e.g. an Activity or a Service)
* #return full apk file path, or null if an exception happened (it should not happen)
*/
public static String getApkName(Context context) {
String packageName = context.getPackageName();
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
String apk = ai.publicSourceDir;
return apk;
} catch (Throwable x) {
}
return null;
}
EDIT
In defense of catch (Throwable x) in this case. At first, now it is well-known that Checked Exceptions are Evil. At second, you cannot predict what may happen in future versions of Android. There already is a trend to wrap checked exceptions into runtime exceptions and re-throw them. (And a trend to do silly things that were unthinkable in the past.) As to the children of Error, well, if the package manager cannot find the apk that is running, it is the kind of problems for which Errors are thrown. Probably the last lines could be
} catch (Throwable x) {
return null;
}
but I do not change working code without testing it.
PackageManager.getPackageInfo() returns information about the package, and PackageInfo.applicationInfo field has required information about the application.
Well, i would like to mark Yuri as the answer but i already knew about that stuff. So I went through each and every option from PackageManager.ApplicationInfo and found .publicSourceDir
So a complete answer with code to my question would be
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pInfo.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So thanks again guys, got my brain goin once again Love StackOverflow!
in above answer need change pInfo to pm
like this
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this answer by Seth

Obtain the Linux UID of an Android App

I would like to be able to get the Linux UID (user ID) of an installed Android application.
Excerpt from Security and Permissions: "At install time, Android gives each package a distinct Linux user ID. The identity remains constant for the duration of the package's life on that device."
Is there a way to retrieve this UID?
adb shell dumpsys package com.example.myapp | grep userId=
Use PackageManager and getApplicationInfo().
The ā€¨packages.xml file present in /data/system
The packages.list file present in /data/system
Contain the list of applications installed and their corresponding UID's.
Use android.os.Process.myUid() to get the calling apps UID directly.
Using the PackageManager is not necessary to find the own UID.
PackageManager packageManager = getPackageManager();
try {
applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
As CommonsWare already wrote, you can use PackageManager to get the UID.
Here's an example:
int uid;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
context.getPackageName(), 0);
uid = info.uid;
} catch (PackageManager.NameNotFoundException e) {
uid = -1;
}
Log.i(LOG_TAG, "UID = " + uid);

How do I get the version number of an application in Android?

We have a set of 3-5 android applications that we have developed for an enterprise to integrate with our back-end. How do we create an installer system that upgrades applications automatically. We were thinking of getting version numbers and querying the backend to get current versions and downloading them.
How do I get the version number of an application in Android?
ApplicationInfo info = getApplicationInfo();
try {
info = getPackageManager().getApplicationInfo(info.packageName, PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Any pointers will be most useful.
Thanks
Sameer
Using the function below you can get the current Version Name or No for the application.
This you can check against that of the app at server side and if needed you can upgrade app.
public static function String getVersionName(Context context, Class cls) {
try {
ComponentName comp = new ComponentName(context, cls);
PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
return pinfo.versionName;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return null;
}
}

Categories

Resources