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

时间:2021-02-18 21:30:47

效果图:

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

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

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

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文件中随便输入一些内容

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

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的资源目录

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

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

    然后在assets目录下放入一个SIMKAL.TTF文件,这个文件是简体中文

    文件下载地址:http://www.font5.com.cn/zitixiazai/1/534.html