Resource files assets and res use different points below the raw file

Source: Internet
Author: User

In the establishment of the project will generally default to establish the assets file, of course, we can also create a raw folder under the Res file, where the surface can be stored in some pictures, audio or text information, can be used in the program, but they have different points;

Assets the following files will not be compiled, and the path can be used to access the contents. The files in raw are automatically compiled, and we can find the corresponding IDs in the R.java file.

See below:

So how do we usually put the resources into these two files?

I personally like to compare the size of the file, if the file is larger than the file will be put into the aeests, because the information in this file is equivalent to the IO stream operation, more time-consuming operation

It is more important to obtain the resource methods in the assets and raw folders:

Assets:assetmanager Assetmanager = Getassets ();

Raw:inputstream InputStream = Getresources (). Openrawresource (R.raw.demo);

The following is the demo activity source code:

[Java]View Plaincopy
  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. * This demo demonstrates how to use files in assets and raw folders
  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. * Use the files in 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. * Use the files in 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 Stream read/write
  47. *
  48. * @param inputstream
  49. * @return ostream.tostring () or "file read and write Failed"
  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 "file read and write failed";
  61. }
  62. }
  63. }

Layout file:

[HTML]View Plaincopy
  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 Run Effect:

Resource files assets and res use different points below the raw file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.