如何访问我放入 res 文件夹中的原始资源?

发布于 2024-09-01 14:06:58 字数 126 浏览 9 评论 0原文

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

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

In J2ME, I've do this like that:
getClass().getResourceAsStream("/raw_resources.dat");

But in android, I always get null on this, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

如果没结果 2024-09-08 14:08:08

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

This worked for for me: getResources().openRawResource(R.raw.certificate)

瘫痪情歌 2024-09-08 14:08:06

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

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

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

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

This will work correctly. Before that you have to create the xml file / text file in raw resource. Then it will be accessible.

Edit
Some times com.andriod.R will be imported if there is any error in layout file or image names. So You have to import package correctly, then only the raw file will be accessible.

迷你仙 2024-09-08 14:08:04

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

通常在 Android 上,您将此类文件放在 assets 目录中。因此,如果您将 raw_resources.dat 放入项目的 assets 子目录中,它将最终位于 APK 的 assets 目录中,并且您可以使用:

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

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

getClass().getResourcesAsStream() works fine on Android. Just make sure the file you are trying to open is correctly embedded in your APK (open the APK as ZIP).

Normally on Android you put such files in the assets directory. So if you put the raw_resources.dat in the assets subdirectory of your project, it will end up in the assets directory in the APK and you can use:

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

It is also possible to customize the build process so that the file doesn't land in the assets directory in the APK.

勿忘初心 2024-09-08 14:08:03
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 文件(您也可以访问其他文件夹)

Ist 2 行是在 onCreate() 方法中编写的 2 行,

其余部分将在扩展 Activity 的类中编写!

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 in linearlayout
hello:: .txt file in res/raw folder (u can access ny othr folder as wel)

Ist 2 lines are 2 written in onCreate() method

rest is to be written in class extending Activity!!

送你一个梦 2024-09-08 14:08:00

Android 访问原始资源

一种高级方法是使用 Kotlin 扩展函数

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

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

例如,您可以以优雅的方式使用输入流,而无需处理异常和内存管理

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

Android access to raw resources

An advance approach is using Kotlin Extension function

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

One more interesting thing is extension function use that is defined in Closeable scope

For example you can work with input stream in elegant way without handling Exceptions and memory managing

fun Context.readRaw(@RawRes resourceId: Int): String {
    return resources.openRawResource(resourceId).bufferedReader(Charsets.UTF_8).use { it.readText() }
}
尹雨沫 2024-09-08 14:07:57

在某些情况下,我们必须使用图像名称而不是生成的 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);

In some situations we have to get image from drawable or raw folder using image name instead if generated 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);
只是一片海 2024-09-08 14:07:54
InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
一页 2024-09-08 14:07:49

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

For raw files, you should consider creating a raw folder inside res directory and then call getResources().openRawResource(resourceName) from your activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文