且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从另一个应用程序访问项目资产?

更新时间:2023-11-22 16:17:58

要获取基本 /资产文件夹,你需要使用AssetsManager列出只有报价的目录

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

将有列出了一些其他文件:图像,声音,WebKit的,也许其他人。您可以忽略这些目录,它们是框架的资产目录中的一部分。这是从 groups.google.com :

目前的资产管理公司合并的   从框架资产目录   随着自己的文件资源   摆在资产。我们应该   可能改变这种行为(这是   一个旧的资源/本地化的一部分   模型),但它并没有做太多的伤害   不同之处在于你会看到更多   在你自己的资产的文件/目录   比你想象的。您的任何   是相同的为一体,在文件   框架资产将被用来代替,   通过访问时的   AssetManager。

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

如果您需要加载这些PNG图像成位图,你也可以做到以下几点:

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

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);