Get path of Android resource
Asked Answered
S

4

40

My question is: is it possible to get the absolute path of a resource (in a subdirectory of the res/ folder) in Android?

Most of the answers I see to this question on google suggest getting a file descriptor, implying that this is not possible.

Is it?

EDIT: The reason I want to do this is because I'm writing classes that accept a path to media and play it. Testing would be much easier if I could pass the absolute path of a resource.

Scincoid answered 10/6, 2011 at 2:52 Comment(3)
you should mark HPP's answer as the correct one. The one you marked is not correctMt
@LeonardoAcevedo done.Scincoid
linked, https://mcmap.net/q/393653/-raw-folder-url-pathEncyclopedia
P
32

Use URI Paths instead of "absolute" path, see this post

Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");

Or use openRawResource(R.id) to open an inputStream, and use it the same way you would use a FileInputStream (readonly)

Perm answered 24/10, 2013 at 19:6 Comment(1)
what kind of file is it?Perm
M
14

Not possible. The resource folder you see is compiled into the apk, and it may not even store as it is. (e.g. those layout xml files are no longer xml files once it is compiled)

Two possible solution/work-around for your situation:

  1. Store the files in the '/assets' directory
  2. Push the file you need to a known location (like /mnt/sdcard/... ) with sdk tools
  3. Create a mockup provider for your media loader that accepts stuff in raw resource rather than path, for your testing purpose
Moulder answered 10/6, 2011 at 3:1 Comment(2)
This doesn't seem correct. I just tried HPP's answer and it works great.April
The answer suggested by HPP is your best approach.Gropius
S
6

In general, NO. As it was repeated several times here, you need to access them through the resources.

That said, there are ways you can work around that:

  • copy the files from res/assets to the internal storage on first run.
  • use adb to push the files to any public location, such as the sdcard (note, it is discouraged also to use absolute paths in there, as not every device may mount the drives to the same location)
  • use a rooted phone, and access any folder, e.g., /data/data/yourapp/ or /tmp/

In any case, you would test against a solution that will most probably not work in most of the devices. I suggest you to follow the directions from the dev guide: http://developer.android.com/guide/topics/data/data-storage.html

Sulfuric answered 10/6, 2011 at 3:4 Comment(1)
Please See HPP'S answer, you wrote your answer 2011, are you still sticking to your answer?Crocker
A
0

Everyone has been giving you the preferred solution and I don't disagree with the advice they are giving you.

You can access the res content directly, but it's a bit tricky. First of all, you need to understand that the res file is stored in your applications base.apk file. That file is a jar file, so you will need to process it using some jar api. The absolute path to this jar file can be obtained from ApplicationInfo. The value of ApplicationInfo.sourceDir is the path to the base.apk file. And by the way, sourceDir is read only. Here's some Java code for getting ApplicationInfo:

    private ApplicationInfo getAppInfo() {
    ApplicationInfo appInfo = null;
    try {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
            appInfo = getApplication().getPackageManager().getApplicationInfo(getApplication().getPackageName(), PackageManager.ApplicationInfoFlags.of(0));
        } else {
            appInfo = getApplication().getPackageManager().getApplicationInfo(getApplication().getPackageName(), PackageManager.GET_META_DATA);
        }
    } catch (PackageManager.NameNotFoundException e) {
        if (appInfo == null) Log.e(TAG, "Package name not found: " + e.getMessage());
    }
    return appInfo;
}

Here's some code that I wrote the I use to copy the content in directory named webContent which was put in as as a directory in a resources file. (IOUtility is my own written code, but it nothing that special)

                    ApplicationInfo appInfo = getAppInfo();
                    String apkPath = appInfo.sourceDir;
                    JarFile apkJar = new JarFile(apkPath);
                    IOUtility.copyResourceDirectory(apkJar, "webContent", webContentDir);
                    apkJar.close();

I've looked at a base.apk file and saw res is available for you to access just like an other jar content. I'll give you a bit of warning, Android compiles some XML content as binary XML. So, XML content might not look like you expect it to look like. You can also access the Android assets directly using this approach. My advice is to use the Android facilities that are designed for accessing this kind of content unless you have some real need to bypass them.

Avigation answered 24/6, 2023 at 0:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.