资源文件assets和 res下面raw文件的使用不同点
2016-01-02 00:47
369 查看
在建立项目中一般会默认建立assets文件,当然我们还可以在res文件下面建立raw文件夹,这里面都可以存放一些图片,音频或者文本信息,可以供我们在程序当中进行使用,不过他们两个也有不同点;
assets下面的文件不会被编译,通过路径可以去访问其中的内容。raw中文件会自动编译,我们可以在R.java文件中找到对应的ID,
看下面截图:
![](http://hi.csdn.net/attachment/201201/8/0_1326028180RS76.gif)
![](http://hi.csdn.net/attachment/201201/8/0_1326016867W82s.gif)
那么既然这样那我们平时该怎么样进行把资源放入这两个文件当中呢?
我个人平时喜欢比较文件的大小,如果文件比较大一点的会放入到aeests文件中,因为用这个文件文件当中的信息,相当于要去进行IO流操作,比较耗时的操作
其中比较重要的是获取到Assets和Raw文件夹中的资源方法:
Assets:AssetManager assetManager = getAssets();
Raw: InputStream inputStream = getResources().openRawResource(R.raw.demo);
下面该Demo的Activity源代码:
[java] view plaincopy
package com.jiangqq.aeesrtandraw;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.EditText;
/**
* 该Demo演示Assets和Raw文件夹中文件的使用方法
*
* @author jiangqq
*
*/
public class AeesrtsAndRawActivity extends Activity {
private EditText et1, et2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
readAssets();
readRaw();
}
/**
* 使用Assets中的文件
*/
private void readAssets() {
et1 = (EditText) findViewById(R.id.et1);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("demo.txt");
et1.setText(read(inputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用Raw中的文件
*/
private void readRaw() {
et2 = (EditText) findViewById(R.id.et2);
InputStream inputStream = getResources().openRawResource(R.raw.demo);
et2.setText(read(inputStream));
}
/**
* 进行IO流读写
*
* @param inputStream
* @return oStream.toString() or “文件读写失败”
*/
private String read(InputStream inputStream) {
try {
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
int length;
while ((length = inputStream.read()) != -1) {
oStream.write(length);
}
return oStream.toString();
} catch (IOException e) {
return "文件读写失败";
}
}
}
布局文件:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/et1" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/et2" />
<EditText
android:id="@+id/et2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Demo运行效果截图:
assets下面的文件不会被编译,通过路径可以去访问其中的内容。raw中文件会自动编译,我们可以在R.java文件中找到对应的ID,
看下面截图:
![](http://hi.csdn.net/attachment/201201/8/0_1326028180RS76.gif)
![](http://hi.csdn.net/attachment/201201/8/0_1326016867W82s.gif)
那么既然这样那我们平时该怎么样进行把资源放入这两个文件当中呢?
我个人平时喜欢比较文件的大小,如果文件比较大一点的会放入到aeests文件中,因为用这个文件文件当中的信息,相当于要去进行IO流操作,比较耗时的操作
其中比较重要的是获取到Assets和Raw文件夹中的资源方法:
Assets:AssetManager assetManager = getAssets();
Raw: InputStream inputStream = getResources().openRawResource(R.raw.demo);
下面该Demo的Activity源代码:
[java] view plaincopy
package com.jiangqq.aeesrtandraw;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.EditText;
/**
* 该Demo演示Assets和Raw文件夹中文件的使用方法
*
* @author jiangqq
*
*/
public class AeesrtsAndRawActivity extends Activity {
private EditText et1, et2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
readAssets();
readRaw();
}
/**
* 使用Assets中的文件
*/
private void readAssets() {
et1 = (EditText) findViewById(R.id.et1);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("demo.txt");
et1.setText(read(inputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用Raw中的文件
*/
private void readRaw() {
et2 = (EditText) findViewById(R.id.et2);
InputStream inputStream = getResources().openRawResource(R.raw.demo);
et2.setText(read(inputStream));
}
/**
* 进行IO流读写
*
* @param inputStream
* @return oStream.toString() or “文件读写失败”
*/
private String read(InputStream inputStream) {
try {
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
int length;
while ((length = inputStream.read()) != -1) {
oStream.write(length);
}
return oStream.toString();
} catch (IOException e) {
return "文件读写失败";
}
}
}
布局文件:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/et1" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/et2" />
<EditText
android:id="@+id/et2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Demo运行效果截图:
![](http://hi.csdn.net/attachment/201201/8/0_1326027945IY5z.gif)
相关文章推荐
- UITableView
- RecyclerView的嵌套使用
- centos 6.7(final)jdk 安装
- ngx_lua应用最佳实践
- 【前端】JS
- 每日记录
- https提供安全的web通讯
- 黑马程序员——阶乘的两种实现方法及水仙花数的打印
- 学校里的那首歌
- [c语言]EmailAddresses(for hw)
- 帧动画的完整实现: 代码直接演示
- C语言 数组长度...int length = sizeof(stu) / sizeof(stu[0]);
- 嵌入式STMFD SP!,{R0-R7,LR}分析
- 概率专题·期望
- Matlab笔记-0
- 学习c++的五十条忠告
- android的照片浏览器(一)至返回所有图片文件
- 从本地文件系统迁移oracle数据库到ASM
- ACM、蓝桥杯等程序竞赛中的一些经验-0
- 使ViewStub 来提高UI的加载的性能