在android开发中,资源文件是我们使用频率最高的,无论是string,drawable,还是layout,这些资源都是我们经常使用到的,而且为我们的开始提供很多方便,不过我们平时接触的资源目录一般都是下面这三个。
除些之外,Android资源文件还有以下三种会经常使用到:
/res/xml
/res/raw
/assets
首先是/res/xml ,这个目录中大家可能偶尔用到过,这里可以用来存储xml格式的文件,并且和其他资源文件一样,这里的资源是会被编译成二进制格式放到最终的安装包里的,我们也可以通过R类来访问这里的文件,并且解析里面的内容。
例如我们在这里存放了一个名为file.xml的文件:
< book >
< title >XML高级编程 </ title >
</ book >
随后,我们就可以通过资源ID来访问并解析这个文件了,如下代码所示:
xml.next();
int eventType = xml.getEventType();
boolean inTitle = false ;
while (eventType != XmlPullParser.END_DOCUMENT) {
// 到达title节点时标记一下
if (eventType == XmlPullParser.START_TAG) {
if (xml.getName().equals("title")) {
inTitle = true ;
}
}
// 如过到达标记的节点则取出内容
if (eventType == XmlPullParser.TEXT && inTitle) {
((TextView)findViewById(R.id.txXml)).setText(
xml.getText()
);
}
xml.next();
eventType = xml.getEventType();
}
在这里,我们用资源类的getXml方法,返回了一个xml解析器,这个解析器的工作原理和SAX方式差不多。
要注意的是,这里的xml文件,最终是会被编译成二进制形式的,如果大家想让文件原样存储的话,那么就要用到下一个目录啦,那就是/res/raw目录这个目录的唯一区别就是,这里的文件会原封不动的存储到设备上,不会被编译为二进制形式,访问的方式也是通过R类,下面是代码示例:
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while (i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
这次使用资源类中的方法,openRawResource,返回给我们一个输入流,这样我们就可以任意读取文件中的内容了,例如上面例子中那样,原样输出文本文件中的内容。
((TextView)findViewById(R.id.txAssets)).setText(readStream(assets.open("data.txt")));
在context上下文中,调用getAssets返回一个AssetManager,然后使用open方法就可以访问需要的资源了,这里open方法是以assets目录为根的。所以上面这段代码访问的是assets目录中名为data.txt的资源文件。
Android文件资源(raw/data/asset)的存取代码如下:
一、私有文件夹下的文件存取(/data/data/包名)
import java.io.FileOutputStream;
import org.apache.http.util.EncodingUtils;
public void writeFileData(String fileName,String message){
try{
FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public String readFileData(String fileName){
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
String res = "";
try{
InputStream in = getResources().openRawResource(R.raw.test1);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
in.close();
}
catch(Exception e){
e.printStackTrace();
}
return res ;
}
三、从asset中获取文件并读取数据(资源文件只能读不能写)
String res="";
try{
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
关于Android获取assets的绝对路径有如下方法:
第一种方法:
String path = "file:///android_asset/文件名";
第二种方法:
InputStream abpath = getClass().getResourceAsStream("/assets/文件名");
若要想要转换成String类型,则使用下列代码:
private byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
}
如果需要获取assets文件夹下的所有文件,可通过如下方法:
String[] files = getAssets().list("");
for(String f : files){
System.out.println(f);
}
} catch (Exception e) {
// TODO: handle exception
}
至于其他的资源存储,详见以下文档,以下是开发文档中的内容:
Android Projects
Android projects are the projects that eventually get built into an .apk
file that you install onto a device. They contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. The following directories and files comprise an Android project:
-
Contains your stub Activity file, which is stored at
src/your/package/namespace/ActivityName.java
. All other source code files (such as.java
or.aidl
files) go here as well. -
Output directory of the build. This is where you can find the final
.apk
file and other compiled resources. - Contains native code sources developed using the Android NDK. For more information, see the Android NDK documentation.
-
Contains the Java files generated by ADT, such as your
R.java
file and interfaces created from AIDL files. -
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an
.apk
file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the theAssetManager
. For example, this is a good location for textures and game data. -
Contains application resources, such as drawable files, layout files, and string values. See
Application Resources for more information.
- For XML files that are compiled into animation objects. See the Animation resource type.
- For XML files that describe colors. See the Color Values resource type.
- For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
- XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
- For XML files that define application menus. See the Menus resource type.
-
For arbitrary raw asset files. Saving asset files here instead of in the
assets/
directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in theR
class. For example, this is a good place for media, such as MP3 or Ogg files. -
For XML files that are compiled into many kinds of resource. Unlike other resources in the
res/
directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into theR
class. -
For miscellaneous XML files that configure application components. For example, an XML file that defines a
PreferenceScreen
,AppWidgetProviderInfo
, or Searchability Metadata. See Application Resources for more information about configuring these application components.
anim/
color/
drawable/
layout/
menu/
raw/
values/
xml/
src/
bin
jni
gen/
assets/
res/
libs/
AndroidManifest.xml
project.properties
local.properties
local.properties
should not be maintained in a source revision control system. If you use Eclipse, this file is not used.
ant.properties
build.xml