歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中資源文件assets和res下面raw文件的使用不同點

Android中資源文件assets和res下面raw文件的使用不同點

日期:2017/3/1 10:39:56   编辑:Linux編程
在建立項目中一般會默認建立assets文件,當然我們還可以在res文件下面建立raw文件夾,這裡面都可以存放一些圖片,音頻或者文本信息,可以供我們在程序當中進行使用,不過他們兩個也有不同點;

assets下面的文件不會被編譯,通過路徑可以去訪問其中的內容。raw中文件會自動編譯,我們可以在R.java文件中找到對應的ID,

看下面截圖:

那麼既然這樣那我們平時該怎麼樣進行把資源放入這兩個文件當中呢?

我個人平時喜歡比較文件的大小,如果文件比較大一點的會放入到aeests文件中,因為用這個文件文件當中的信息,相當於要去進行IO流操作,比較耗時的操作

其中比較重要的是獲取到Assets和Raw文件夾中的資源方法:

Assets:AssetManager assetManager = getAssets();

Raw: InputStream inputStream = getResources().openRawResource(R.raw.demo);

下面該Demo的Activity源代碼:

[java]
  1. package com.jiangqq.aeesrtandraw;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import Android.app.Activity;
  6. import android.content.res.AssetManager;
  7. import android.os.Bundle;
  8. import android.widget.EditText;
  9. /**
  10. * 該Demo演示Assets和Raw文件夾中文件的使用方法
  11. *
  12. * @author jiangqq
  13. *
  14. */
  15. public class AeesrtsAndRawActivity extends Activity {
  16. private EditText et1, et2;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. readAssets();
  22. readRaw();
  23. }
  24. /**
  25. * 使用Assets中的文件
  26. */
  27. private void readAssets() {
  28. et1 = (EditText) findViewById(R.id.et1);
  29. AssetManager assetManager = getAssets();
  30. try {
  31. InputStream inputStream = assetManager.open("demo.txt");
  32. et1.setText(read(inputStream));
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. /**
  38. * 使用Raw中的文件
  39. */
  40. private void readRaw() {
  41. et2 = (EditText) findViewById(R.id.et2);
  42. InputStream inputStream = getResources().openRawResource(R.raw.demo);
  43. et2.setText(read(inputStream));
  44. }
  45. /**
  46. * 進行IO流讀寫
  47. *
  48. * @param inputStream
  49. * @return oStream.toString() or “文件讀寫失敗”
  50. */
  51. private String read(InputStream inputStream) {
  52. try {
  53. ByteArrayOutputStream oStream = new ByteArrayOutputStream();
  54. int length;
  55. while ((length = inputStream.read()) != -1) {
  56. oStream.write(length);
  57. }
  58. return oStream.toString();
  59. } catch (IOException e) {
  60. return "文件讀寫失敗";
  61. }
  62. }
  63. }

布局文件:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/et1" />
  14. <EditText
  15. android:id="@+id/et1"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content" />
  18. </LinearLayout>
  19. <LinearLayout
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:orientation="horizontal" >
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/et2" />
  27. <EditText
  28. android:id="@+id/et2"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content" />
  31. </LinearLayout>
  32. </LinearLayout>
Demo運行效果截圖:

這樣就OK了。

Copyright © Linux教程網 All Rights Reserved