在ProviderTestCase2中找不到Android资源

时间:2013-04-02 11:52:38

标签: android android-contentprovider android-resources android-testing

我正在为我的应用程序编写一个csv-file-importer,但是我在编写测试时遇到了困难。我要做的是导入一个示例csv文件并将结果与​​数据库进行比较。

public class CSVImportTest extends ProviderTestCase2<MyProvider> {

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mContentResolver = getMockContentResolver();
        setContext(new IsolatedContext(mContentResolver, getContext()));
        mContext = getContext();
        mCSVImport = new CSVImportParker(mContext);

    }

    public void read() {
        try {
            // Fails here with "File not found."
            InputStream input = mContext.getResources()
                 .openRawResource(my.package.R.raw.file);

            ...
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }

        ...
    }

}

永远找不到测试文件,尽管它位于正确的位置。

1 个答案:

答案 0 :(得分:0)

问题是原始目录中的资源是压缩的,除非它们具有ogg或mp3文件扩展名。请参阅此说明:

Proguard breaking audio file in assets or raw

docs

This function only works for resources that are stored in the package as uncompressed data, which typically includes things like mp3 files and png images.

因此,解决此问题的最简单方法是将mp3或ogg文件扩展名添加到原始资源中。它不干净或漂亮,但它的工作原理。