【问题标题】:Android Resource not found in ProviderTestCase2在 ProviderTestCase2 中找不到 Android 资源
【发布时间】:2013-04-02 11:52:38
【问题描述】:

我目前正在为我的应用程序编写一个 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();
        }

        ...
    }

}

从未找到测试文件,尽管它位于正确的位置。

【问题讨论】:

  • 你的问题到底出在哪里?
  • @Ekonion 我的问题是我的测试文件永远找不到,尽管它存在于正确的位置。此外,我不确定这是否是测试此类事情的正确方法,因为我通常是 TDD 新手。
  • 我正在尝试解决类似的问题。你怎么知道文件实际上存在于“正确的位置”?
  • @prakt 你能告诉我们被捕获的异常吗?

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


【解决方案1】:

问题是原始目录中的资源被压缩,除非它们具有 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 文件扩展名添加到您的原始资产。它不干净也不漂亮,但它确实有效。

【讨论】:

    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2018-08-12
    • 2019-04-14
    • 2018-12-07
    • 2021-05-22
    相关资源
    最近更新 更多