Differences between the use of raw files in Android source files assets and res

Source: Internet
Author: User

when creating a project, the assets file is created by default, of course, we can also create raw folders under the res file, which can store some images, audio or text information, it can be used in Program , but there are also differences between them.

files under assets are not compiled and can be accessed through paths. Raw files are automatically compiled. You can find the corresponding ID in the R. Java file,

See the following:

In this case, how do we put resources into these two files?

I personally prefer to compare the file size. If the file is larger, It will be placed in the aeests file. Because the information in this file is used, it is equivalent to performing Io stream operations, time-consuming operations

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

Assets: assetmanager = getassets ();

Raw: inputstream = getresources (). openrawresource (R. Raw. Demo );

The following demo ActivitySource code:

Package Com. jiangqq. aeesrtandraw;

ImportJava. Io. bytearrayoutputstream;
ImportJava. Io. ioexception;
ImportJava. Io. inputstream;

ImportAndroid. App. activity;
ImportAndroid. content. res. assetmanager;
ImportAndroid. OS. Bundle;
ImportAndroid. widget. edittext;

/**
* This demo demonstrates how to use files in the assets and raw folders.
*
*@ AuthorJiangqq
*
 */
Public ClassAeesrtsandrawactivityExtendsActivity {
PrivateEdittext ET1, et2;

@ Override
Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Readassets ();
Readraw ();
}

/**
* Use files in assets
*/
Private Void Readassets (){
ET1 = (edittext) findviewbyid (R. Id. ET1 );
Assetmanager = getassets ();
Try {
Inputstream = assetmanager. Open ("demo.txt ");
Et1.settext (read (inputstream ));
} Catch (Ioexception e ){
E. printstacktrace ();
}
}

/**
* use raw files
*/
private void readraw () {
et2 = (edittext) findviewbyid (R. id. et2);
inputstream = getresources (). openrawresource (R. raw. demo);
et2.settext (read (inputstream);
}

/**
* Read and write Io streams
*
*@ ParamInputstream
*@ ReturnOstream. tostring () or "file read/write failed"
*/
PrivateString read (inputstream ){

Try {
Bytearrayoutputstream ostream =New Bytearrayoutputstream ();
Int Length;
While (Length = inputstream. Read ())! =-1 ){
Ostream. Write (length );
}
Return Ostream. tostring ();
} Catch (Ioexception e ){
Return "File read/write failed ";
}
}
}

 

Layout file:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
Android: Orientation = "Vertical"   >

linearlayout
Android: layout_width =" fill_parent "
Android: layout_height =" wrap_content "
Android: orientation = "horizontal" >

textview
Android: layout_width =" wrap_content "
Android: layout_height =" wrap_content "
Android: text =" @ string/ET1 " />

edittext
Android: ID =" @ + ID/ET1 "
Android: layout_width = "fill_parent"
Android: layout_height =" wrap_content " />
linearlayout >

linearlayout
Android: layout_width =" fill_parent "
Android: layout_height =" wrap_content "
Android: orientation = "horizontal" >

textview
Android: layout_width =" wrap_content "
Android: layout_height =" wrap_content "
Android: text =" @ string/et2 " />

edittext
Android: ID =" @ + ID/et2 "
Android: layout_width = "fill_parent"
Android: layout_height =" wrap_content " />
linearlayout >

</Linearlayout>Demo running effect:

In this case, OK.

URL: http://greatverve.cnblogs.com/archive/2012/01/09/android-assets-res.html

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.