【问题标题】:adding bin files in android studio在android studio中添加bin文件
【发布时间】:2015-04-13 10:06:11
【问题描述】:

我有一个 bin 文件 en-parser-chunking.bin,它在我的项目中执行分块部分。我按如下方式使用它

              InputStream modelInParse = null;
              try {
                   //load chunking model
                     Log.i(TAG,"1");
                     modelInParse = new FileInputStream("en-parser-chunking.bin");

但这会在 android studio 中引发 IO 异常。 我的项目文件中也有它。

【问题讨论】:

  • 我无法添加任何文件,包括文本文件..
  • 帮我看看有什么问题..?

标签: android exception android-studio bin


【解决方案1】:

可能是这样的

InputStream modelInParse = null;
              try {
                   //load chunking model
                     Log.i(TAG,"1");
                     modelInParse = new FileInputStream("Relative/or/absolute/path/of/your/.bin/file");

系统无法找到您的文件。所以它给出了 IO 异常。请正确给出文件路径

【讨论】:

  • 同样的异常 04-13 15:50:48.604 21404-21404/com.example.sravankumar.vbnlirs I/HARSHA: 1 04-13 15:50:48.619 21404-21404/com.example .sravankumar.vbnlirs D/HARSHA:[04-13 15:50:48.619 21404:21404 I/HARSHA] 例外
  • 一般IO异常的主要原因是无法打开/定位指定的文件。所以再想一想并做出相应的更正。
  • android可以添加bin文件吗??因为它在 Eclipse 中运行良好。我想让它成为一个 android 应用程序,它会抛出异常。
  • 您可以将文件保存在资产文件夹中。您可以使用 AssetManager 在您的项目中使用它们
【解决方案2】:
  • 将您的 .bin 文件保存在resources 文件夹中(我在单元测试中使用.bin 文件,所以我将它保存在app-name/app/src/test/resources 中)
  • 在你的课堂上用ClassLoader打开文件:

    public InputStream openBin(String filename) throws IOException { lassLoader classLoader = this.getClass().getClassLoader(); File file = new File(classLoader.getResource(filename).getPath()); InputStream input = new FileInputStream(file); return input; }

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    相关资源
    最近更新 更多