如何在 android 的原始文件夹中加载 *.mat 文件

How to load *.mat file in raw folder of android

我尝试使用 JMatIO 在 Android 4.4 中加载一个 .mat 文件。

.mat 文件在外部存储中时它运行良好,但当文件在应用程序的 raw 文件夹中时它不起作用。目前,应用程序找不到任何 *.mat 个文件。日志只打印消息 "fail to load matrix file".

下面是我使用的代码:

try {
        mfr = new MatFileReader("android.resource://" + getPackageName() + "/raw/rm.mat");
} catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "fail to load matrix file");
        System.exit(0);
}
if (mfr != null) {
        Log.d(TAG, "Success to load matrix file");
}

如何才能成功加载文件?

您似乎无法使用以下代码正确访问文件:

mfr = new MatFileReader("android.resource://" + getPackageName() + "/raw/rm.mat");

据我了解,您正在尝试访问位于 \res\raw\ 文件夹中的文件。这通常使用 R.raw.filname_without_extension 来完成。在您的情况下,您应该尝试:

mfr = new MatFileReader(getResources().openRawResource(R.raw.rm), MatFileType.Regular);

如果您仍有问题,请查看这些资源:

  1. 文档:Accessing Resources

    Access to Original Files

    While uncommon, you might need access your original files and directories. If you do, then saving your files in res/ won't work for you, because the only way to read a resource from res/ is with the resource ID. Instead, you can save your resources in the assets/ directory.

    Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources. Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.

    However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource()

  2. JMatIO 来源:The relevant MatFileReader signature

  3. 所以问题:R.raw.anything cannot be resolved
  4. 所以问题:How to get File from R.raw
  5. 所以问题:How to read file from res/raw by name