图书馆项目中使用了哪些资产?

时间:2012-08-30 12:00:52

标签: android assets library-project

我的一个图书馆项目中有一些神奇的东西。从库项目本身获取布局(res / layout)时,assets文件夹中的文件将从调用项目(而不是库项目)中获取。

我目前正在图书馆项目中构建帮助活动。调用项目为库项目中的活动提供文件名。这些文件存储在调用项目中 - 而不是存储在库项目中。

在库项目中使用AssetsManager时,它使用调用项目中的文件 - 而不是库项目的assets文件夹。

这是正确的行为吗?

以下是根项目中的精简Activity:

// Calling Project
package aa.bb.aa;

public class MyListActivity extends ListActivity {

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.mylistactivity); // <--- Stored in resources of the calling project
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.men_help) {
            Intent intent = new Intent(this, aa.bb.bb.MyFileBrowser.class);
            intent.putExtra(MyConstants.FILE, "mylistactivity.html"); // <-- Stored in assets of the calling project
            startActivityForResult(intent, MyConstants.DIALOG_HELP);
            return true;
        }

        return super.onOptionsItemSelected(menuItem);
    }
}

这是图书馆项目中的活动。我认为资产是从这里获取的,而不是来自调用的Activity / Project:

// Library project
package aa.bb.bb;

    public class MyFileBrowser extends Activity {

        @Override
        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);

            setContentView(R.layout.myfilebrowser); // <-- Stored in resources of the library project

            webView = (WebView) findViewById(R.id.browser);

            Locale locale = Locale.getDefault();
            String localeLanguage = (locale.getLanguage() != null) ? locale.getLanguage() : "en";

            Bundle bundleExtras = getIntent().getExtras();
            if (bundleExtras != null) {
                String file = bundleExtras.getString(MyConstants.FILE);
                if (!StringUtils.isEmpty(file)) {
                    String filename = "help-" + localeLanguage + File.separator + file;

                    AssetManager assetManager = getAssets();
                    InputStream inputStream = null;
                    try {
                        inputStream = assetManager.open(filename);
                    } catch (IOException ioException) {
                        filename = "help-en" + File.separator + file;
                    } finally {
                        try {
                            if (inputStream != null) {
                                inputStream.close();
                            }
                        } catch (IOException ioException) {
                        }
                    }

                    webView.loadUrl("file:///android_asset" + File.separator + filename); // <-- Uses file in assets of calling project - not library project
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

找到答案。资产总是从引用项目中使用。图书馆项目无法保存资产。我在这个页面上找到了它:

Library Projects

以下是描述此内容的文档部分:

  

这些工具不支持使用原始资产文件(保存在   资源/目录)在库项目中。使用的任何资产资源   应用程序必须存储在的assets /目录中   应用项目本身。但是,资源文件保存在res /中   目录是受支持的。