开发者

Access Assests from another application?

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 fo开发者_StackOverflow社区lder 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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜