eclipse 是否可以在 Android 库项目中读取没有上下文引用的原始文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9855197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:53:25  来源:igfitidea点击:

Is it possible to read a raw text file without Context reference in an Android library project

androideclipseandroid-resources

提问by Hong

I could put a text file in folder res\raw of a library project, but reading it seems to require a Context reference. Could anyone shed some light on this?

我可以将一个文本文件放在一个库项目的文件夹 res\raw 中,但阅读它似乎需要一个 Context 引用。任何人都可以对此有所了解吗?

回答by yorkw

Check out my answer hereto see how to read file from POJO.

在此处查看我的答案以了解如何从 POJO 读取文件。

Generally, the res folder should be automatically added into project build path by ADT plugin. suppose you have a test.txt stored under res/raw folder, to read it without android.content.Context:

一般情况下,res文件夹应该由ADT插件自动添加到项目构建路径中。假设您在 res/raw 文件夹下存储了一个 test.txt,可以在没有 android.content.Context 的情况下读取它:

String file = "raw/test.txt"; // res/raw/test.txt also work.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);

I did this before with an old SDK version, it should work with latest SDK as well. Give it a try and see if this helps.

我以前用旧的 SDK 版本这样做过,它也应该适用于最新的 SDK。试一试,看看这是否有帮助。

回答by Radu Dan

In order to access a resource you need a context. See here the definition of Context.class from the developer.android site

为了访问资源,您需要一个上下文。从 developer.android 站点查看 Context.class 的定义

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

有关应用程序环境的全局信息的接口。这是一个抽象类,其实现由Android系统提供。它允许访问特定于应用程序的资源和类,以及调用应用程序级操作,例如启动活动、广播和接收意图等。

So, through a context you can access a resource file. You can create another class and pass the context from an activity to it. Create a method that reads a specified resource file.

因此,您可以通过上下文访问资源文件。您可以创建另一个类并将上下文从活动传递给它。创建一个读取指定资源文件的方法。

For example:

例如:

public class ReadRawFile {
    //Private Variable
    private Context mContext;

    /**
     * 
     * Default Constructor
     * 
     * @param context activity's context
     */
    public ReadRawFile(Context context){
        this.mContext = context;
    }

    /**
     * 
     * @param str input stream used from readRawResource function
     * @param x integer used for reading input stream
     * @param bo output stream
     */
    private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){
        //not hitting end
        while(x!=-1){
            //write to output buffer
            bo.write(x);
            try {
                //read next
                x = str.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * @return output file to string
     */
    public String readRawResource(){
        //declare variables
        InputStream rawUniversities = mContext.getResources().openRawResource(R.raw.universities);
        ByteArrayOutputStream bt = new ByteArrayOutputStream();
        int universityInteger;

        try{
            //read/write
            universityInteger = rawUniversities.read();
            writeBuffer(rawUniversities, universityInteger, bt);

        }catch(IOException e){
            e.printStackTrace();
        }
        //return string format of file
        return bt.toString();
    }

}