Access Assests from another application?
Asked Answered
O

3

7

I get a lot of requests in my application to allow for custom icons packs from BetterCut / Open Home. The way it seems to work is you install BetterCut or Open Home, then you can install tons of these free icon packs from the market. Once installed both those apps (and other apps) will poll for those icon packs and use the icons.

I want to know how to poll the install applications for the asset folders that are available. I have opened up a few of the icon packs and verified that there is an assets folder in there and they are full of all the icon png files.

I've searched on here, other code sites, google, etc but havn't found any leads.

UPDATE:

From the answer below I have written some code to try and list a file from my own projects assets directory but it does not seem to work.

Resources r = this.getResources();
AssetManager a = r.getAssets();
String[] list = a.list("/");
Log.d("test", "Length of / is "+list.length);
for (String s : list) {
    Log.d("test", s);
}

Log.d("test", "Length of /assets is "+a.list("/assets").length);
Log.d("test", "Length of /assets/ is "+a.list("/assets/").length);
Log.d("test", "Length of /assets/ is "+a.list("/assets/").length);
Log.d("test", "Length of ./assets/ is "+a.list("./assets/").length);
Log.d("test", "Length of ./assets is "+a.list("./assets").length);

This is the output:

03-16 12:25:04.591: DEBUG/test(13526): Length of / is 6
03-16 12:25:04.591: DEBUG/test(13526): AndroidManifest.xml
03-16 12:25:04.591: DEBUG/test(13526): META-INF
03-16 12:25:04.591: DEBUG/test(13526): assets
03-16 12:25:04.591: DEBUG/test(13526): classes.dex
03-16 12:25:04.591: DEBUG/test(13526): res
03-16 12:25:04.591: DEBUG/test(13526): resources.arsc
03-16 12:25:04.614: DEBUG/test(13526): Length of /assets is 0
03-16 12:25:04.637: DEBUG/test(13526): Length of /assets/ is 0
03-16 12:25:04.661: DEBUG/test(13526): Length of /assets/ is 0
03-16 12:25:04.692: DEBUG/test(13526): Length of ./assets/ is 0
03-16 12:25:04.716: DEBUG/test(13526): Length of ./assets is 0

UPDATE 2 99% There!!!:

I figured out that you can read from the assets directory without actually using the folder name:

InputStream is = assetManager.open("test.png");

I also tried this with an asset in Appliction 2 from Application 1, where the folder path is /asset/icon/image.png:

InputStream is = assetManager.open("icon/image.png");

Next I figured out that you can list a directory inside assets:

String[] list = assetManager.list("icons");

That also works great. The only thing failing right now is how to list the base directory assets.

Occiput answered 16/3, 2010 at 13:4 Comment(0)
R
4

To get the base /assets folder you need to use the AssetsManager to list the directory with just quotes:

AssetManager am = this.getAssets();
String[] names = am.list("");

There will be some additional files listed: images, sounds, webkit, maybe others. You can ignore these directories, they are part of the frameworks assets directory. This is a quote from groups.google.com:

Currently the asset manager merges the asset directory from the framework resources along with your own files placed in "assets". We should probably change this behavior (it was part of an old resource/ localization model), but it doesn't do much damage except that you see more files/directories in your own assets than you might expect. Any of your files that are the same as one in the framework assets will be used instead, when accessed through your AssetManager.

You can also list a subfolder inside the assets directory and do not need any slashes:

String[] names= am.list("subfolder");

Note that I did not include "/assets" in the filename. Finally once you have your list of files you can load them in like:

InputStream in = am.open("file.png");

That will load in a file in the base assets folder. Or you can load a file in a subfolder like this:

InputStream in = am.open("subfolder/file.png");

If you need to load those pngs into a Bitmap you can also do the following:

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Repugn answered 16/3, 2010 at 20:54 Comment(1)
This seems to do the trick, thanks! Also funny is the quote about the framework assets merging with the assets folder is by a Google employee Hackbod, which is the same person that didn't provide the answer on here.Occiput
L
3

The Resources object gives you access to assets. PackageManager can give you access to the Resources for an application.

Lajuanalake answered 16/3, 2010 at 14:9 Comment(3)
I figured I should crawl before I walked and added a file to my projects assets folder and now I am trying to list it using the AssetManager but it seems like it is a no go when trying to list the assets directory. I will update the original post with the code I am trying.Occiput
See #1496085Lajuanalake
I tried using that persons exact "displayFiles" method and put in a print to see the length of the list, which is still zero for all of the tests except for the root folder "/". I never get a list of the files in "/assets" to be able to list them out. I also tried going right to a file in my assets folder with: InputStream is = a.open("/assets/test.png"); That throws a file not found exception.Occiput
S
1

You should probably get a basic overview of how resources and assets work before digging into things. :)

First, an .apk is just a zip file, so you all you want to do is dig through its contents you can put that file on your host computer and use all of the normal zip tools to look at it.

Second, there are two main types of user supplied files in an .apk: raw assets, and structured resources.

The former is just an arbitrary hierarchy of files that can be retrieved with http://developer.android.com/reference/android/content/res/AssetManager.html#open(java.lang.String)

The latter are processed by the built tools into a structured table, allowing them to vary by configuration. They are accessed using Resources and related classes. Drawables, strings, colors, and most of the other types of data you retrieve from an .apk is usually stored as a resource, not an asset.

You can use the aapt tool to see all of the resources in an .apk: aapt dump resources

Finally, if you want to read the data from these .apks, how about looking at their information on how to build them? That should give you a lot of very basic information that you need on how they are structured.

Santamaria answered 16/3, 2010 at 18:14 Comment(1)
I do not need to access the file on my computer. I need to access the assets directory in application 2 from application 1 on the phone itself. I get a boatload of requests to add support for the BetterCut icon packs that are free on the market. So from my app I want the user to be able to select the icons from there (they are stored in the assets folder, I looked). Most of the things you mention are not really relevant to the original problem. I don't need a guide for building apks, extracting images or using Drawables or other resources inside my own application from the res folder.Occiput

© 2022 - 2024 — McMap. All rights reserved.