Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Assests from another application?

Tags:

android

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.

like image 955
Cameron McBride Avatar asked Mar 16 '10 13:03

Cameron McBride


People also ask

How do I access an asset file?

To view asset files in Files home, select Libraries and then select Asset Library . To view and edit asset files in Setup, enter Asset Files in the Quick Find box, then select Asset Files.

Can one Android application access the data of another application?

Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.

How do I access assets on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

What is the use of Assets folder in Android?

Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data. If one wants to access data untouched, Assets are one way to do it.


2 Answers

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);
like image 143
Mark B Avatar answered Sep 21 '22 07:09

Mark B


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

like image 34
CommonsWare Avatar answered Sep 20 '22 07:09

CommonsWare