简体   繁体   English

getResourceAsStream()返回null

[英]getResourceAsStream() returning null

I have a java .jar library, and from there i'm trying to access a file of my Android project with: 我有一个Java .jar库,从那里我试图通过以下方式访问我的Android项目的文件:

InputStream inputStream = getClass().getResourceAsStream("/folder/data.txt");
someMethod(inputStream); // inputStream is null

But the inputStream is always null . 但是inputStream始终为null I'm confused about where should i put my /folder/data.txt in order to be able to find it from the .jar. 我对应该将/folder/data.txt放在/folder/data.txt才能从.jar中找到它感到困惑。

From within the .jar project i can find the data.txt, doing the following in eclipse: 在.jar项目中,我可以在eclipse中执行以下操作找到data.txt:

MyProject > properties > java build path > libraries > Add class folder

and this code: 和此代码:

InputStream inputStream = getClass().getResourceAsStream("/data.txt"); // without the folder

This is the structure of the Java Project: 这是Java项目的结构:

MyJavaProject

  - src
    - com.example
      - something.java
      - folder
           - data.txt

Now, inside the Android Project i try to do the same but i keep getting null (inputStream): 现在,在Android Project中,我尝试执行相同的操作,但我不断得到null(inputStream):

MyAndroidProject

  - src
    - several packages
    - folder
       - data.txt
  - libs
     - my jar

Where should i put my data.txt? 我应该将data.txt放在哪里? or should i change something in my .jar 还是应该在我的.jar文件中进行更改

I've read a lot of similar questions but i haven't found a solution. 我已经阅读了很多类似的问题,但是还没有找到解决方案。 I've also read about getResourceAsStream() but i'm obviously not getting it. 我也读过有关getResourceAsStream()但我显然没有得到。

getResourceAsStream() isn't terribly useful in android. getResourceAsStream()在android中并不是非常有用。 It refers to files embedded in a JAR or classpath container, not android Resources. 它引用嵌入JAR或类路径容器中的文件,而不是android资源。 You probably want to use the android assets feature instead. 您可能想改用android assets功能。 You can simply place the asset file ( "data.txt" ) under your project's assets/ directory. 您可以简单地将资产文件( "data.txt" )放在项目的assets/目录下。

To get an InputStream for the asset, you can use the AssetsManager , like this (error handling is omitted for clarity: 要获取资产的InputStream ,可以使用AssetsManager ,如下所示(为清楚起见,省略了错误处理:

// get the assets manager from your activity or `Context`
final AssetsManager am = getAssets();
final InputStream   in = am.open("data.txt");
// do stuff with the input stream

Another approach is to use Resources instead, but this depends on the contents of data.txt Resources are intended for data types that android knows how to load and parse, such as drawables, strings, and various xml files. 另一种方法是改为使用Resources ,但这取决于data.txt的内容data.txt适用于android知道如何加载和解析的数据类型,例如可绘制对象,字符串和各种xml文件。 Assets are for raw data that you are going to read and parse yourself. 资产用于原始数据,您将要阅读和解析自己。

Resouses in android means drawables, values .. etc folders. android中的Resouses表示可绘制对象,值.. etc文件夹。

For text file create a folder named raw and put your data file there. 对于文本文件,请创建一个名为raw的文件夹,然后将您的数据文件放在此处。

Try to send the path to the library method as as data.txt then as raw/data.txt 尝试以data.txt然后以raw / data.txt的形式发送库方法的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM