Article source: blog.csdn.net/developer_j…
You can also create a raw folder under the RES file, which can store some pictures, audio or text information for use in the program. However, there are some differences between the two files.
Files under assets are not compiled and can be accessed through a path. The raw file is automatically compiled, and we can find the corresponding ID in the r.java file.
Take a look at the screenshot below:
So in that case how do we normally put resources into these two files?
Personally, I like to compare file sizes. If the files are larger, I put them into AEests because the information in these files is equivalent to doing I/O stream operations, which are time-consuming
The most important method is to obtain Assets from Assets and Raw folders:
Assets:AssetManager assetManager = getAssets();
Raw: InputStream inputStream = getResources().openRawResource(R.raw.demo);
The following Demo Activity source code:
[java] view plain copy
- package com.jiangqq.aeesrtandraw;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import android.app.Activity;
- import android.content.res.AssetManager;
- import android.os.Bundle;
- import android.widget.EditText;
- / * *
- * This Demo shows how to use files in Assets and Raw folders
- *
- * @author jiangqq
- *
- * /
- public class AeesrtsAndRawActivity extends Activity {
- private EditText et1, et2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- readAssets();
- readRaw();
- }
- / * *
- * Use files within Assets
- * /
- private void readAssets() {
- et1 = (EditText) findViewById(R.id.et1);
- AssetManager assetManager = getAssets();
- try {
- InputStream inputStream = assetManager.open(“demo.txt”);
- et1.setText(read(inputStream));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- / * *
- * Use files from Raw
- * /
- private void readRaw() {
- et2 = (EditText) findViewById(R.id.et2);
- InputStream inputStream = getResources().openRawResource(R.raw.demo);
- et2.setText(read(inputStream));
- }
- / * *
- * Read and write IO streams
- *
- * @param inputStream
- * @return ostream.tostring () or “file read/write failed”
- * /
- private String read(InputStream 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:
[html] view plain copy
- The < LinearLayout XMLNS: android = “schemas.android.com/apk/res/and…
- 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>
Screenshot of Demo operation effect:
That’s OK.
\