android 从assets和res中读取文件(转)
1. 相关文件夹介绍
|
目录Directory |
资源类型Resource Types |
|
res/anim/ |
XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 |
|
res/drawable/ |
.png、.9.png、.jpg文件,它们被编译进以下的Drawable资源子类型中: 要获得这种类型的一个资源,可以使用Resource.getDrawable(id) 为了获取资源类型,使用mContext.getResources().getDrawable(R.drawable.imageId) 注意:放在这里的图像资源可能会被aapt工 具自动地进行无损压缩优化。比如,一个真彩色但并不需要256色的PNG可能会被转换为一个带调色板的8位PNG。这使得同等质量的图片占用更少的资源。 所以我们得意识到这些放在该目录下的二进制图像在生成时可能会发生变化。如果你想读取一个图像位流并转换成一个位图(bitmap),请把图像文件放在 res/raw/目录下,这样可以避免被自动优化。 |
|
res/layout/ |
被编译为屏幕布局(或屏幕的一部分)的XML文件。参见布局声明(Declaring Layout) |
|
res/values/ |
可以被编译成很多种类型的资源的XML文件。 注意: 不像其他的res/文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。XML元素类型控制这些资源应该放在R类的什么地方。 尽管这个文件夹里的文件可以任意命名,不过下面使一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中): array.xml 定义数组 colors.xml 定义color drawable和颜色的字符串值(color string values)。使用Resource.getDrawable()和Resources.getColor()分别获得这些资源。 dimens.xml定义尺寸值(dimension value)。使用Resources.getDimension()获得这些资源。 strings.xml定义字符串(string)值。使用Resources.getString()或者Resources.getText()获取这些资源。getText()会保留在UI字符串上应用的丰富的文本样式。 styles.xml 定义样式(style)对象。 |
|
res/xml/ |
任意的XML文件,在运行时可以通过调用Resources.getXML()读取。 |
|
res/raw/ |
直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。 |
2.自动生成的R class
3. 在代码中使用资源
- // Load a background for the current screen from a drawable resource.
- this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
- // WRONG Sending a string resource reference into a
- // method that expects a string.
- this.getWindow().setTitle(R.string.main_title);
- // RIGHT Need to get the title from the Resources wrapper.
- this.getWindow().setTitle(Resources.getText(R.string.main_title));
- // Load a custom layout for the current screen.
- setContentView(R.layout.main_screen);
- // Set a slide in animation for a ViewFlipper object.
- mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
- R.anim.hyperspace_in));
- // Set the text on a TextView object.
- TextView msgTextView = (TextView)findViewByID(R.id.msg);
- msgTextView.setText(R.string.hello_message);
- //在屏幕上显示标准应用程序的图标
- public class MyActivity extends Activity {
- public void onStart() {
- requestScreenFeatures(FEATURE_BADGE_IMAGE);
- super.onStart();
- setBadgeResource(android.R.drawable.sym_def_app_icon);
- }
- }
- //应用系统定义的标准"绿色背景"视觉处理
- public class MyActivity extends Activity
- public void onStart() {
- super.onStart();
- setTheme(android.R.style.Theme_Black);
- }
- }
4. xml文件内引用资源
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, HelloDemo!</string>
- </resources>
5. 替换资源(为了可替换的资源和配置)
6. Color Value
- <color name="color_name">#color_value</color>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="opaque_red">#f00</color>
- <color name="translucent_red">#80ff0000</color>
- </resources>
7.Color Drawables
- <drawable name="color_name">color_value</drawable>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <drawable name="opaque_red">#f00</drawable>
- <drawable name="translucent_red">#80ff0000</drawable>
- </resources>
8. 图片
9. dimension
- <dimen name="dimen_name">dimen_value单位</dimen>
Java: float dimen = Resources.getDimen(R.dimen.some_name)
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <dimen name="one_pixel">1px</dimen>
- <dimen name="double_density">2dp</dimen>
- <dimen name="sixteen_sp">16sp</dimen>
- </resources>
10. string
- //不使用转义符则需要用双引号包住整个string
- <string name="good_example">"This'll work"</string>
- //使用转义符
- <string name="good_example_2">This\'ll also work</string>
- //错误
- <string name="bad_example">This won't work!</string>
- //错误 不可使用html转义字符
- <string name="bad_example_2">XML encodings won't work either!</string>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="simple_welcome_message">Welcome!</string>
- <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
- </resources>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textAlign="center"
- android:text="@string/simple_welcome_message"/>
- // Assign a styled string resource to a TextView on the current screen.
- CharSequence str = getString(R.string.styled_welcome_message);
- TextView tv = (TextView)findViewByID(R.id.text);
- tv.setText(str);
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="search_results_resultsTextFormat">%1$d results for <b>&quot;%2$s&quot;</b></string>
- </resources>
- //title是我们想赋值给%2$s的字符串
- String escapedTitle = TextUtil.htmlEncode(title);
- String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
- String resultsText = String.format(resultsTextFormat, count, escapedTitle);
- CharSequence styledResults = Html.fromHtml(resultsText);
11. assets文件夹资源的访问
需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作
以下为从Raw文件中读取:
代码
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下为直接从assets读取
代码
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
当然如果你要得到内存流的话也可以直接返回内存流!
android 从assets和res中读取文件(转)的更多相关文章
- android 从assets和res中读取文件
1. 相关文件夹介绍 在Android项目文件夹里面,主要的资源文件是放在res文件夹里面的.assets文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像xml,java文件被预编译, ...
- 用adb pull命令从android系统中读取文件失败的原因及解决办法
问题:使用adb pull命令从android系统中读取文件失败.显示:Permission denied 原因:是由于文件权限原因引起. 使用ls -l命令查看android系统中的 ...
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
- Java中读取文件
Java中读取文件,去除一些分隔符,保存在多维数组里面 public void readFile(String filePath) { File file=new File(filePath); Ar ...
- 如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件
如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件? 目的:就是希望在项目的配置文件中配上一样的路径,不管协作者使用的是什么平台,都能够读到文件. 比如:L ...
- PHP中读取文件的几个方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- R中读取文件,找不到路径问题 No such file or directory
R中读取文件,找不到路径问题 No such file or directory 近日,读取文件时.出现例如以下问题 > passenger = read.csv('internationa ...
- php中读取文件内容的几种方法。(file_get_contents:将文件内容读入一个字符串)
php中读取文件内容的几种方法.(file_get_contents:将文件内容读入一个字符串) 一.总结 php中读取文件内容的几种方法(file_get_contents:将文件内容读入一个字符串 ...
- Spring 中读取文件-ResourceLoaderAware
Spring 中读取文件-ResourceLoaderAware 概述 Spring ResourceLoader为我们提供了一个统一的getResource()方法来通过资源路径检索外部资源.从而将 ...
随机推荐
- 显示log里的ansi codecs颜色字符
方法: vim AnsiEsc插件 http://www.vim.org/scripts/script.php?script_id=302 less -r cat和tail命令都可以正常显示,而且ta ...
- mounted钩子问题
recommend.vue <script type="text/ecmascript-6"> import Slider from 'base/slider/slid ...
- 解决 The file will have its original line endings in your working directory
首先出现这个问题主要原因是:我们从别人github地址上通过git clone下载下来,而又想git push到我们自己的github上,那么就会出现上面提示的错误信息 此时需要执行如下代码: git ...
- CStatic设置位图
CStatic 用于显示位图 如果要显示图标,则必须要设置窗口属性为 SS_BITMAP 和 SS_CENTERIMAGE,实例代码如下: //获得指向静态控件的指针 CStatic *pStatic ...
- Spring Boot 集成 JWT 实现单点登录授权
使用步骤如下:1. 添加Gradle依赖: dependencies { implementation 'com.auth0:java-jwt:3.3.0' implementation('org.s ...
- Django - 创建多对多及增加示例
创建多对多: 方式一: 自定义关系表 备注:自定义表Host.Application,通过自定义表,将表Host和Application进行关联(通过外键方式工): 执行语句:python manag ...
- 1 JSONP
一.什么是跨域访问举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请求 ...
- Vector 二维数组 实现
1.C++实现动态二维数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 2.利用指针数组实现二 ...
- HDU-5968异或密码
超级传送门 题目描述: 晨晨在纸上写了一个长度为N的非负整数序列{ai}.对于这个序列的一个连续子序列{al,al+1,…,ar}晨晨可以求出其中所有数异或的结果 alxoral+1xor...xor ...
- 51.percentiles rank以及网站访问时延SLA统计
主要知识点: percentile_ranks的用法 percentile的优化 一.percentile_ranks的用法 SLA:就是所提供的服务的标准. 比如一个网站的提供的访问延时的S ...