Read raw files ., Read raw files

Source: Internet
Author: User

Read raw files ., Read raw files

Some androiddevelopers have files such as audio, video, HTML, and other files that do not want the compiler to compile and keep the original appearance packaged into the apk file (this is very common and common in game development, such as the game music, graphics, and other resources used by the game ), you can use the res/raw and assets in the res directory of Android to save them. Files in the res/raw and assets directories will not be compiled into binary files by Android. Android will pack these file resources in the original and intact format into the apk files during the final compilation and release.
Res/raw and res/assets have similarities and differences.
(1) files in the res/raw directory will be mapped to R by Android. id. xxx resource. You can access the file resources in the raw directory through R. id. filename to obtain the ing, and then perform file operations. However, this is not true for res/assets. Files in the res/assets Directory will not be R. id. the file storage and access model in the res/assets Directory is similar to the read/write model of the Java file system.
(2) The file structure in the assets directory can be hierarchical or tree-like, while res/raw cannot be hierarchical. In other words, you can create a multi-level, tree-like file directory structure in assets, and the file resources under res/raw must be tiled under the res/raw directory.
(3) file size. The size of a single file in the res/raw directory can be as large as several MB, but the size of a single file in the res/assets file directory should be controlled below MB as much as possible, otherwise, problems may occur.
(4) Access to file resources under the res/assets Directory is generally as follows (as shown in Appendix 1 ):
AssetManager am = getAssets ();
InputStream is = am. open ("filename ");
Access resources in the res/assets Directory through AssetManager.
Access to file resources in the res/raw directory is generally as follows:
InputStream is = getResources (). openRawResource (R. id. filename );
The file resources in the res/raw directory can also be obtained first by URI positioning:
Uri uri = Uri. parse ("android. resource: //" + getPackageName () + "/" + R. raw. filename );

1 package com. lixu. rawduxie; 2 3 import java. io. bufferedInputStream; 4 5 import java. io. byteArrayOutputStream; 6 import java. io. IOException; 7 import java. io. inputStream; 8 import android. app. activity; 9 import android. OS. bundle; 10 import android. widget. toast; 11 12 public class MainActivity extends Activity {13 14 @ Override15 protected void onCreate (Bundle savedInstanceState) {16 super. onCreate (sav EdInstanceState); 17 setContentView (R. layout. activity_main); 18 19 readraw (); 20} 21 22 private void readraw () {23 // read the byte stream of the file from the raw file 24 InputStream is = getResources (). openRawResource (R. raw. lixutxt); 25 26 try {27 byte [] buffer = readByteFromInputstream (is); 28 String s = new String (buffer, 0, buffer. length, UTF-8); 29 Toast. makeText (getApplicationContext (), s, 0 ). show (); 30 31} catch (IOException e ){ 32 e. printStackTrace (); 33} 34 35} 36 37 private byte [] readByteFromInputstream (InputStream is) throws IOException {38 39 BufferedInputStream bis = new BufferedInputStream (is ); 40 41 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 42 43 int BYTE_SIZE = 2*1024; 44 // create a byte array as the cache 45 byte [] B = new byte [BYTE_SIZE]; 46 47 int c; 48 // c = bis. read (B) reads the byte stream and places it in byte array B, baos. write (B, 0, c); write as much as read 49 wh Ile (c = bis. read (B ))! =-1) {50 baos. write (B, 0, c); 51 baos. flush (); 52} 53 // convert byte array to byte array 54 byte [] date = baos. toByteArray (); 55 56 is. close (); 57 baos. close (); 58 59 return date; 60 61} 62 63}

 

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.