Android Learning note 39--using raw resources

Source: Internet
Author: User

Raw Resources

There is no type file in Android that specifically provides administrative support, which is called the original resource. For example: sound resources ...

Android Raw Resource storage location:

The 1.res/raw,android SDK processes the original resources in the directory and generates a unique index in the R manifest

2./assets/, the resources under this directory are more thorough primitive resources. Android uses Assetmanager to manage raw resources in this directory

Note: The unique indexes generated by the original resource through the SDK are used in the same way as other resources are used. eg:r.raw.filename/@raw. FileName

Assetmanager is a manager class that specializes in managing raw resources under the/assets/directory, which provides the following common ways to access assets resources:

1.InputStream Open (String filename): Gets the input stream corresponding to the original resource according to the file name

2.AssetFileDescriptor openfd (String fileName): Obtaining the original resource according to the file name the Assetfiledescriptor.assetfiledescriptor represents a description of the original resource that the application can use to obtain the original resource.

Examples: How Res/raw and/assets/are used:

Operation Steps:

1. Put different audio files in raw and assets directories respectively

2. Start with code processing ....

Layout file = = "<linearlayout xmlns:android=" http://schemas.android.com/apk/res/android "xmlns:tools="/http Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:gravi Ty= "Center_horizontal" android:orientation= "Horizontal" tools:context= ". Mainactivity "> <button android:id=" @+id/btnraw "android:layout_width=" Wrap_content "Android : layout_height= "wrap_content" android:text= "raw"/> <button android:id= "@+id/btnassets" Android Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "assets"/> &lt ; Button android:id= "@+id/btnstop" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:text=" Stop "/></linearlayout> code implementation = =" Package Com.example.myoriginalresources;import Java.io.I Oexception;import Android.media.mediaplayer;import android.os.bundle;import Android.App. Activity;import Android.content.res.assetfiledescriptor;import Android.content.res.assetmanager;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends activity{mediaplayer Mediaraw = null; MediaPlayer mediaassets = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Mediaraw = Mediaplayer.create (this, R.RAW.RAWQ); Assetmanager am = getassets (); try{//gets the assetfiledescriptorassetfiledescriptor afd = AM.OPENFD ("Assets.mp3") corresponding to the specified file; Mediaassets = new MediaPlayer ();//use MediaPlayer to load the specified MP3 file Mediaassets.setdatasource (afd.getfiledescriptor ()); Mediaassets.prepare ();} catch (IOException e) {e.printstacktrace ();} Button Btnraw = (button) This.findviewbyid (R.id.btnraw); Button btnassets = (button) This.findviewbyid (r.id.btnassets); Button Btnstop = (button) This.findviewbyid (r.id.btnstop); Btnraw.setonclicklistener (new ButtonClick ()); Btnassets.setonclicklistener (new ButtonClick ()); Btnstop.setonclicklistener (new ButtonClick ());} Class ButtonClick implements onclicklistener{@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btnraw: Mediaraw.start (); break;case R.id.btnassets:mediaassets.start (); break;case r.id.btnstop:mediaassets.stop (); Mediaraw.stop (); finish (); break;}}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is pre Sent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

  

Android Learning note 39--using raw resources

Related Article

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.