Android开发 ---如何操作资源目录中的资源文件5 ---Raw资源管理与国际化
效果图:




1、activity_main.xml
描述:
定义两个按钮,一个是Raw资源管理,一个是处理国际化语言,其中i18n表示简体中文
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Raw资源管理"
android:onClick="test_5"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/i18n"
android:onClick="test_5"
/>
</LinearLayout>
2、MainActivity.java
描述:
页面跳转
package com.example.android_shaperesoucesdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void test_5(View view){
Intent intent = new Intent(this,RawActivity.class);
startActivity(intent);
}
}
3、activity_raw.xml
描述:
定义两个按钮,一个实现读取Raw资源文件中的文本资源;一个实现读取Raw资源文件中的音频资源
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_raw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showMessage"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取Raw中文本资源"
android:onClick="readTxt"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取Raw中音频资源"
android:onClick="readMp3"
/>
</LinearLayout>
4、RawActivity.java
package com.example.android_shaperesoucesdemo;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; import java.io.IOException;
import java.io.InputStream; public class RawActivity extends Activity {
private TextView showMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_raw);
showMessage = (TextView)findViewById(R.id.showMessage);
}
//读取文本资源
public void readTxt(View view){
//定义一个输入流,读取raw文件中的hello文件
//Android读取asserts和raw文件夹下的文件 经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件
InputStream input = getResources().openRawResource(R.raw.hello);
try {
//获取流的大小
int size = input.available();
//将流转换为字节
byte[] bytes = new byte[size];
//读出字节
input.read(bytes);
input.close();
//将字节转换成字符串显示在UI界面上
showMessage.setText(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
//定义一个读取MP3文件资源的方法
public void readMp3(View view) throws IOException{
//MediaPlayer是播放音频和视频的组件
//通过组件获取raw中的音乐文件nobody
MediaPlayer mp = MediaPlayer.create(this,R.raw.nobody);
//开始播放音乐
mp.start();
}
}
5、res目录下创建一个raw包,在包中创建一个文本文件hello.txt,并在包中放一首音乐nobody.mp3
在hello.txt文件中随便输入一些内容

6、处理国际化:
在res资源目录下创建一个values-zh-rCN的包,包中创建一个String.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android_Resouces_2</string> <!--定义字符串-->
<string name="i18n">国际化</string>
</resources>
在main目录下创建一个assets的资源目录


然后在assets目录下放入一个SIMKAL.TTF文件,这个文件是简体中文
文件下载地址:http://www.font5.com.cn/zitixiazai/1/534.html
Android开发 ---如何操作资源目录中的资源文件5 ---Raw资源管理与国际化的更多相关文章
- Android开发---如何操作资源目录中的资源文件4 ---访问xml的配置资源文件的内容
Android开发---如何操作资源目录中的资源文件4 XML,位于res/xml/,这些静态的XML文件用于保存程序的数据和结构. XmlPullParser可以用于解释xml文件 效果图: 描述: ...
- Android开发---如何操作资源目录中的资源文件3--圆角边框、背景颜色渐变效果、边框颜色
Android开发---如何操作资源目录中的资源文件3 效果图 1.圆角边框 2.背景颜色渐变效果 1.activity_main.xml 描述: 定义了一个shape资源管理按钮 <?xml ...
- Android开发 ---如何操作资源目录中的资源文件2
Android开发 ---如何操作资源目录中的资源文件2 一.颜色资源管理 效果图: 描述: 1.改变字体的背景颜色 2.改变字体颜色 3.改变按钮颜色 4.图像颜色切换 操作描述: 点击(1)中的颜 ...
- Android开发---如何操作资源目录中的资源文件
效果图: 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...
- android开发之-查看、编辑手机sqlite数据库文件-实测
效果图: 1.开始——运行——输入cmd ,输入adb shell,错误:一是“adb不是内部命令或外部命令,也不是可运行的程序或批处理文件”,二是“error:device not found”. ...
- 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world
一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...
- 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来
/*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...
- python实现在目录中查找指定文件的方法
python实现在目录中查找指定文件的方法 本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 代码如下: import os from ...
- 如何查找一个目录中所有c文件的总行数
如何查找一个目录中所有c文件的行数 面试题问到了一题,如何统计wc文件夹下所有文件的行数,包括了子目录. 最后在 https://blog.csdn.net/a_ran/article/details ...
随机推荐
- 雷林鹏分享:C# 常量
C# 常量 常量是固定值,程序执行期间不会改变.常量可以是任何基本数据类型,比如整数常量.浮点常量.字符常量或者字符串常量,还有枚举常量. 常量可以被当作常规的变量,只是它们的值在定义后不能被修改. ...
- English Voice of <<See You Again >>
<See You Again >(<当我们再相见>) 演唱:Wiz Khalifa/Charlie Puth 维兹·卡利法/查理·普斯 It's been a long da ...
- LeetCode--367--有效的完全平方数
问题描述: 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入:16 输 ...
- apiCloud 调微信支付,调支付宝支付
data里面的参数信息,需要从后台接口中调取,点击查看微信支付详情,https://docs.apicloud.com/Client-API/Open-SDK/wxPay 首先,需要在config.x ...
- 模拟curl函数
只要需要调用微信的网址,就需要模拟curl请求 $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_cr ...
- PHP单例模式实例,连接数据库对类的引用
<?php//单例模式连接数据库class pzhang{ static private $instance; private static $config; private $dbase = ...
- aboutme and my goal
active, diligent ,work hard now,I am a sophomore,I must workhard ,ecspacially my major ,so , pass CE ...
- CF-517C-思维/math
http://codeforces.com/contest/1072/problem/C 题目大意是给出两个数a,b ,找出若干个数p,使得 SUM{p}<=a ,找出若干个数q使得SUM{q} ...
- CRM ORDER_MAINTAIN
H: GUID CRMT_OBJECT_GUID RAW CRM 订单对象的 GUID BP_NUMBER BU_PARTNER 业务伙伴编号 FIRSTNAME BU_NAMEP_F 业务伙伴(人员 ...
- 类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”等一些问题的解决方案
有些时候我们引用了一些32位的dll,结果就会出现类似“未能加载文件或程序集“tesseractengine3”或它的某一个依赖项”这样的问题,原因是IIS的应用程序池的设置中默认是不启用32位的应用 ...