【问题标题】:How to get access to raw resources that I put in res folder?如何访问我放在 res 文件夹中的原始资源?
【发布时间】:2011-02-20 20:01:24
【问题描述】:

在 J2ME 中,我是这样做的: getClass().getResourceAsStream("/raw_resources.dat");

但是在android中,我总是得到null,为什么?

【问题讨论】:

  • android 和 J2ME 编程之间存在巨大差异
  • 在一个关于在 Android 和 J2ME 上运行相同的东西的问题上看到这样的评论很有趣。看我的回答。

标签: android java-me resources inputstream android-resources


【解决方案1】:

对于原始文件,您应该考虑在 res 目录中创建一个原始文件夹,然后从您的活动中调用 getResources().openRawResource(resourceName)

【讨论】:

  • Samuh,感谢您的回复,如果我这样做,我会遇到错误 Android XML 格式问题,因为我使用的不是标准 xml,还有其他方法可以将其用作真正的原始数据吗?
  • 非标准 XML 是什么意思?您也可以在资产中捆绑 XML 文件。
  • Samuh,我正在移植 j2me 应用程序,它有扩展名为 *.xml 的文件,这些文件的内容与 xml 非常相似,但不一样,eclipse 对这些文件大喊大叫,因为它可以不要编译它。也许我可以在这个文件上设置某种“忽略”?但我不知道怎么做..
  • res/raw 中的文件不会被资源编译器以任何方式处理,因此不会因为它们的内容而产生错误。
  • @Guy:当你有不同的 R 时的常见问题。您根本没有引用正确的 R。检查您的导入或引用具有资源的正确 R。
【解决方案2】:
InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));

【讨论】:

  • 请看 Samuh 的回答(下),更准确。
  • 附带说明,原始文件夹不允许通过文件名访问。有些应用需要动态文件名,所以在这种情况下assets 文件夹很有用
  • 他没有问资产/他问的是 res/raw/
  • 这不是 Android 的做法。检查 Samuh 的答案。
  • 他说的是 res 文件夹,而不是 assets 文件夹。
【解决方案3】:
TextView txtvw = (TextView)findViewById(R.id.TextView01);
        txtvw.setText(readTxt());

 private String readTxt()
    {
    InputStream raw = getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try
    {
        i = raw.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = raw.read();
        }
        raw.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }


    return byteArrayOutputStream.toString();

}

TextView01:: 线性布局中的txtview hello:: res/raw 文件夹中的 .txt 文件(你也可以访问任何其他文件夹)

前 2 行是 2 写在 onCreate() 方法中

rest 写在扩展Activity 的类中!!

【讨论】:

    【解决方案4】:

    在某些情况下,我们必须使用图像名称而不是生成的 id 从可绘制或原始文件夹中获取图像

    // Image View Object 
            mIv = (ImageView) findViewById(R.id.xidIma);
    // create context Object for  to Fetch  image from resourse 
    Context mContext=getApplicationContext();
    
    // getResources().getIdentifier("image_name","res_folder_name", package_name);
    
    // find out below example 
        int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName());
    
    // now we will get contsant id for that image       
            mIv.setBackgroundResource(i);
    

    【讨论】:

    • 这一行: int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName());是金
    【解决方案5】:

    InputStream in = getResources().openRawResource(resourceName);

    这将正常工作。在此之前,您必须在原始资源中创建 xml 文件/文本文件。然后就可以访问了。

    编辑
    如果布局文件或图像名称有任何错误,有时会导入com.andriod.R。所以你必须正确导入包,然后才能访问原始文件。

    【讨论】:

    • openRawResource() 需要一个 int,而不是一个字符串 developer.android.com/reference/android/content/res/…
    • 这是否也适用于可绘制文件夹中存在的资源?
    • 使用“R.raw.NameOfResource”作为整数。
    • 即使该方法需要一个 int,我也能找到带有 R.raw.filename(不带扩展名)的文件
    【解决方案6】:

    getClass().getResourcesAsStream() 在 Android 上运行良好。只需确保您尝试打开的文件已正确嵌入到您的 APK 中(以 ZIP 格式打开 APK)。

    通常在 Android 上,您将此类文件放在 assets 目录中。所以如果你把raw_resources.dat放在你项目的assets子目录下,它最终会在APK的assets目录下,你可以使用:

    getClass().getResourcesAsStream("/assets/raw_resources.dat");
    

    还可以自定义构建过程,使文件不会出现在 APK 的 assets 目录中。

    【讨论】:

    • 嘿,不错。甚至不需要上下文。
    • 需要注意的是assets文件夹位于src/main/assets
    【解决方案7】:

    一种先进的方法是使用 Kotlin Extension function

    fun Context.getRawInput(@RawRes resourceId: Int): InputStream {
        return resources.openRawResource(resourceId)
    }
    

    一个更有趣的事情是扩展函数 use 定义在 Closeable 范围内

    例如,您可以优雅地处理输入流,而无需处理异常和内存管理

    fun Context.readRaw(@RawRes resourceId: Int): String {
        return resources.openRawResource(resourceId).bufferedReader(Charsets.UTF_8).use { it.readText() }
    }
    

    【讨论】:

      【解决方案8】:

      这对我有用:getResources().openRawResource(R.raw.certificate)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-24
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多