Storage and reading of Res/raw files

Source: Internet
Author: User

Usually, if the Android developer has some files such as audio, video,. Html,.mp3, etc. these files do not want the compiler to compile and keep the original originals packaged into the APK file (which is common and common in game development, such as game music, graphs and other resources), Then you can use Android in the Res directory Res/raw save. Files in the Res/raws directory will not be compiled by Android into binary, and Android will keep these files intact and packaged into the APK file at the end of the compilation release.

How to read raw files:

1  PackageCom.zzw.testraw;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.ByteArrayOutputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.util.Log; One  A  Public classMainactivityextendsActivity { -  -     Private Static FinalString TAG = "Mainactivity"; the  - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); +         //Setcontentview (r.layout.activity_main); -  + Readraw (); A     } at  -     Private voidReadraw () { -InputStream is =getresources (). Openrawresource (R.raw.hello); -  -         Try { -             byte[] data =Readbytedatafrominputstream (IS); inString content =NewString (data, 0, Data.length, "UTF-8")); - log.d (TAG, content); to}Catch(IOException e) { + e.printstacktrace (); -         } the  *     } $ Panax Notoginseng     Private byte[] Readbytedatafrominputstream (InputStream is)throwsIOException { -Bufferedinputstream bis =NewBufferedinputstream (IS); the  +Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); A  the         Final intBuffer_size = 2 * 1024; +  -         intc = 0; $         byte[] buffer =New byte[buffer_size]; $  -         //the reason for writing baos.write (buffer, 0, c) is how much to read -          while((c = bis.read (buffer))! =-1) { theBaos.write (buffer, 0, c); - Baos.flush ();Wuyi         } the  -         byte[] data =Baos.tobytearray (); Wu Baos.flush (); -  About baos.close (); $ is.close (); -  -         returndata; -  A     } +}

Storage and reading of Res/raw files

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.