2012-08-30 31 views
0

我的一个图书馆项目中有一些神奇的事情发生。虽然布局(res/layout)是从库项目本身获取的,但assets文件夹中的文件是从调用项目(而不是库项目)中获取的。图书馆项目中使用了哪些资产?

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

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

这是正确的行为?

下面是根项目的精简调用活动:

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

而这里的在库项目的活动。我认为资产是从这里获取的,而不是从调用活动/项目:

// 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 
       } 
      } 
     } 
    } 

回答

0

找到了答案。资产始终从参考项目中使用。图书馆项目不能持有资产。我发现这个网页上:

Library Projects

这里是文档的一部分,它说明这一点:

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