【问题标题】:Is it possible to read a raw text file without Context reference in an Android library project是否可以在 Android 库项目中读取没有上下文引用的原始文本文件
【发布时间】:2012-04-08 22:58:33
【问题描述】:

我可以将文本文件放在库项目的 res\raw 文件夹中,但读取它似乎需要上下文引用。有人可以对此有所了解吗?

【问题讨论】:

    标签: android eclipse android-resources


    【解决方案1】:

    查看我的回答here,了解如何从 POJO 读取文件。

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

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

    我之前使用旧的 SDK 版本进行过此操作,它也应该适用于最新的 SDK。试一试,看看是否有帮助。

    【讨论】:

    • 感谢您的提示。我刚试过。 Eclipse 不喜欢将文件放在 res 下,所以我让文件留在 res\raw 中。尝试通过 this.getClass().getClassLoader().getResourceAsStream("text.txt") 或 this.getClass().getClassLoader().getResourceAsStream("raw\text.txt")
    • @Hong,我已经在我的 Mac 上使用 SDK r16 进行了尝试,现在我可以确认“res/raw/test.txt”和“raw/test.txt”都有效。而“test.txt”抛出 NPE。请注意,您需要斜杠 (/),而不是反斜杠 (\)。
    • 是的!!!有用!它也可以在静态构造函数中工作,因此不必在每次调用该方法时都执行此类读取。非常感谢。
    • 非常感谢您的测试。我也在使用r16。 InputStream in = Myclass.class.getClassLoader().getResourceAsStream("res/raw/txt.txt") 有效,但 InputStream in = Myclass.class.getClassLoader().getResourceAsStream("raw/txt.txt") 返回 null。
    • 是的。为了确保,我刚刚再次尝试使用或不使用前导斜杠。这是在图书馆里。像 MyApp 引用 MyClass 之类的东西,它位于具有文件 txt.txt 的库项目中。
    【解决方案2】:

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

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

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

    例如:

    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();
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。根据我对您的回答的理解,没有上下文就无法阅读原始文本。
    【解决方案3】:

    2021 年可以获取没有上下文的原始文件

    val Int.rawString: String
        get(){
            var res= ""
            val `is`: InputStream = Resources.getSystem().openRawResource(this)
            val baos = ByteArrayOutputStream()
            val b = ByteArray(1)
            try {
                while (`is`.read(b) !== -1) {
                    baos.write(b)
                }
                res = baos.toString()
                `is`.close()
                baos.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            return res
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 2012-01-15
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      相关资源
      最近更新 更多