Android开发程序员今日看点

Android 资源文件基本使用

2016-10-21  本文已影响0人  shellever

assets和raw目录中资源的区别

相同点

不同点

注意:

assets和raw目录中资源的访问

读取res/raw目录下的资源文件linewalker.mp3,通过以下方式获取输入流来进行写操作:

InputStream is = getResources().openRawResource(R.raw.linewalker.mp3);
*   ######  [*A*pple](#a "苹果")
*   ######  [*B*lackberry](#b "黑莓")
*   ######  [*C*oconut](#c "椰子")
*   ######  [*D*urian](#d "榴莲")
*   ######  [*E*lder berry](#e "接骨木果")
*   ######  [*F*ig](#f "无花果")
*   ######  [*G*rape](#g "葡萄")
*   ######  [*H*oney-dew melon](#h "哈密瓜")
*   ######  [*I*yokan](#i "伊予柑")
*   ######  [*J*uicy peach](#j "水蜜桃")
*   ######  [*K*iwi](#k "奇异果")

使用以每次读取一行的方式来读取整个文件:

public String getResourceFromRawByReadLine(int resId) {
    InputStreamReader inputReader = null;
    BufferedReader bufReader = null;
    String result = "";
    try {
        inputReader = new InputStreamReader(getResources().openRawResource(resId));
        bufReader = new BufferedReader(inputReader);
        String line;
        while ((line = bufReader.readLine()) != null) {
            result += line + "\n";
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputReader != null) {
                inputReader.close();
            }
            if (bufReader != null) {
                bufReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

显示结果:

res_raw_read_line.jpg
7. 两个妇女在聊天,聊起自己大读大学的孩子。A:我每个月给女儿寄八百元,可女儿总是钱说不够用。不知道她都干了些什么,可真够让人担心的。B:我女儿从来都不给家里要钱,才让人担心呢。

使用以每次读取一个字节的方式来读取整个文件:

public String getResourceFromRawByReadByte(int resId) {
    String result = "";
    InputStream in = null;
    ByteArrayOutputStream bos = null;
    try {
        in = getResources().openRawResource(resId);
        bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[10 * 1024];    // 10kB
        int len;
        while ((len = in.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        result = bos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

显示结果:

res_raw_read_byte.jpg

使用资源文件id来初始化MediaPlayer:

public void initMediaPlayer(int resId) {
    mMediaPlayer = MediaPlayer.create(this, resId);
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            isCompletion = true;
        }
    });
}

显示效果:

res_raw_resid_start.jpg res_raw_resid_pause.jpg

读取assets目录下的文件资源health.txt,通过以下方式获取输入流来进行写操作:

InputStream is = getResources().getAssets().open("health.txt");

or

AssetManager am = getAssets();  
InputStream is = am.open("health.txt");  
1 、巧用牙膏:若有小面积皮肤损伤或烧伤、烫伤,抹上少许牙膏,可立即止血止痛,也可防止感染,疗效颇佳。  

2 、巧除纱窗油腻:可将洗衣粉、吸烟剩下的烟头一起放在水里,待溶解后,拿来擦玻璃窗、纱窗,效果均不错。  

3 、将虾仁放入碗内,加一点精盐、食用碱粉,用手抓搓一会儿后用清水浸泡,然后再用清水洗净,这样能使炒出的虾仁透明如水晶,爽嫩可口。  

4 、和饺子面的窍门1:在1斤面粉里掺入6个蛋清,使面里蛋白质增加,包的饺子下锅后蛋白质会很快凝固收缩,饺子起锅后收水快,不易粘连  

5 、将残茶叶浸入水中数天后,浇在植物根部,可促进植物生长;把残茶叶晒干,放到厕所或沟渠里燃熏,可消除恶臭,具有驱除蚊蝇的功能。  
...

使用读字节流的方式来读取文件的全部内容并显示:

public String getResourceFromAssets(String fileName){
    String result = "";
    InputStream in = null;
    try {
        in = getResources().getAssets().open(fileName);
        byte[] data = readRawFromStream(in);
        if(data != null) {
            result = new String(data);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

public static byte[] readRawFromStream(InputStream fis) {
    byte[] result = null;
    ByteArrayOutputStream bos = null;
    try {
        bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[10 * 1024];        // 10kB
        int len;
        while ((len = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        result = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

调用getResourceFromAssets函数来读取assets/health.txt:

mContentTextView = (TextView) findViewById(R.id.tv_content);
mContentTextView.setText(getResourceFromAssets("health.txt"));

显示结果:

res_assets.jpg

HTML字符实体的引用

<string name="text_item">项目</string>
<string name="text_number">数值</string>
<string name="text_unit">单位</string>
<string name="text_temp">温度</string>
<string name="text_price">价格</string>

<!--HTML Character Entities-->
<string name="html_char_yen">¥</string>
<string name="html_char_deg">°</string>
mTempTv.setText(R.string.text_temp);
float tempNumber = 26.8f;
String tempNumberString = String.format(Locale.getDefault(), "%.1f", tempNumber);
mTempNumberTv.setText(tempNumberString);
String char_deg = getResources().getString(R.string.html_char_deg) + "C";
mTempUnitTv.setText(char_deg);

mPriceTv.setText(R.string.text_price);
float priceNumber = 99.88f;
String priceNumberString = String.format(Locale.getDefault(), "%.2f", priceNumber);
mPriceNumberTv.setText(priceNumberString);
String char_yen = getResources().getString(R.string.html_char_yen);
mPriceUnitTv.setText(char_yen);
html_char_entity_string.jpg

常用的HTML字符转义查询:

注意:

<string name="html_char_apos">\'</string>
<string name="chat_welcome">I\'m pixie, and very glad to serve you.</string>

Android字符串国际化

i18n_zh_rCN_strings.png

两个文件的内容如下:

values/strings.xml

<resources>
    <string name="app_name">I18N</string>

    <string name="text_show">Show</string>
    <string name="text_content">Confidence of success is almost success.</string>
</resources>

values-zh-rCN/strings.xml

<resources>
    <string name="app_name">国际化</string>

    <string name="text_show">显示</string>
    <string name="text_content">对成功抱有信心,就近乎成功。</string>
</resources>
i18n_show_en.jpg i18n_show_cn.jpg

语言代码表查询:

源码下载:

ResourceFileOperation

上一篇 下一篇

猜你喜欢

热点阅读