Android 如何访问我放在 res 文件夹中的原始资源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2856407/
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
Android how to get access to raw resources that i put in res folder?
提问by Arkaha
In J2ME, I've do this like that:
getClass().getResourceAsStream("/raw_resources.dat");
在 J2ME 中,我是这样做的:
getClass().getResourceAsStream("/raw_resources.dat");
But in android, I always get null on this, why?
但是在android中,我总是在这方面得到null,为什么?
采纳答案by Adrian Vintu
InputStream raw = context.getAssets().open("filename.ext");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
回答by Samuh
For raw files, you should consider creating a raw folder inside res directory and then call getResources().openRawResource(resourceName)
from your activity.
对于原始文件,您应该考虑在 res 目录中创建一个原始文件夹,然后getResources().openRawResource(resourceName)
从您的活动中调用。
回答by sravan
In some situations we have to get image from drawable or raw folder using image name instead if generated id
在某些情况下,如果生成 id,我们必须使用图像名称从 drawable 或 raw 文件夹中获取图像
// 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);
回答by yoAlex5
An advance approach is using Kotlin Extension function
一种先进的方法是使用 Kotlin扩展功能
fun Context.getRawInput(@RawRes resourceId: Int): InputStream {
return resources.openRawResource(resourceId)
}
One more interesting thing is extension function usethat is defined in Closeable scope
更有趣的一件事是在 Closeable 范围内定义的扩展函数使用
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() }
}
回答by poojan9118
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)
TextView01:: txtview in linearlayout hello:: res/raw 文件夹中的 .txt 文件(你也可以访问 ny othr 文件夹)
Ist 2 lines are 2 written in onCreate() method
2 行是用 onCreate() 方法编写的 2 行
rest is to be written in class extending Activity!!
其余的要写在扩展Activity的类中!!
回答by 0xF
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).
getClass().getResourcesAsStream()
在 Android 上运行良好。只需确保您尝试打开的文件正确嵌入到您的 APK 中(以 ZIP 格式打开 APK)。
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:
通常在 Android 上,您将此类文件放在assets
目录中。因此,如果您将 放在项目raw_resources.dat
的assets
子目录中,它将最终assets
位于 APK中的目录中,您可以使用:
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.
还可以自定义构建过程,使文件不会assets
出现在 APK的目录中。
回答by java dev
InputStream in = getResources().openRawResource(resourceName);
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.
这将正常工作。在此之前,您必须在原始资源中创建 xml 文件/文本文件。然后就可以访问了。
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.
编辑
有时如果布局文件或图像名称有任何错误,将导入 com.andriod.R。因此,您必须正确导入包,然后才能访问原始文件。