Code is the best teacher

1. An overview of the

  1. The assets directory is used to store the original files of the app
  2. All files in assets will not be compiled and will be packaged as is into APK
  3. Use the specific utility class AssetManager to read files in the Assets directory
  4. Usually 1-text; 2- no compression image; 3-H5 mixed development related documents; 4- Audio and video files
  5. Unlike res/raw, files under assets cannot be referenced using R.xxX. ID
methods function
list(String path) Obtain the names of files and folders under path
open(String fileName) Open the file in ACCESS_STREAMING mode by default
open(String fileName, int accessMode) Specify open mode. There are

Not specified: ACCESS_UNKNOWN

Random: ACCESS_RANDOM

Order: ACCESS_STREAMING

Cache: ACCESS_BUFFER
close() Disabling Resource Recovery

2. Use

Load the text.txt

Loading pictures

Load the h5

2.1 Loading Text

 private void readAssetTxt(a) {
        String result = "";
        try {
            InputStream is = getAssets().open("word.txt");
            int lenght = is.available();
            byte[]  buffer = new byte[lenght];
            is.read(buffer);
            result = new String(buffer, "utf8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTv.setText("word.txt:\n"+result);
    }
Copy the code

2.2 Loading images


    private void readAssetImg(a) {
        Bitmap bitmap = null;
        try {
            InputStream is = getAssets().open("img/ic_test.png");
            bitmap = BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bitmap ! =null){ mImgShow.setImageBitmap(bitmap); }}Copy the code

Load the HTML 2.1


    private void readAssetH5(a) {
        String fileName = "file:///android_asset/index.html";
        mTv.setText(fileName);

        WebViewClient webViewClient = new WebViewClient();
        mWebView.setWebViewClient(webViewClient);
        mWebView.loadUrl(fileName);
    }

Copy the code

3. Pay attention to

  1. Avoid storing large files, which will directly affect the APK package size
  2. It is recommended that you use Assets to store files in mixed development situations
  3. If you need a direct reference, you can also use /res/raw

4. Other code

activity.java

public class MainActivity extends AppCompatActivity {

    TextView mTv;
    ImageView mImgShow;
    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_read_assets_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { readAssetTxt(); }}); findViewById(R.id.btn_read_assets_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { readAssetImg(); }}); findViewById(R.id.btn_read_assets_h5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { readAssetH5(); }}); mTv = findViewById(R.id.tv_content); mImgShow= findViewById(R.id.img_show); mWebView= findViewById(R.id.web_view); checkPermission(); }private void readAssetH5(a) {
        String fileName = "file:///android_asset/index.html";
        mTv.setText(fileName);

        WebViewClient webViewClient = new WebViewClient();
        mWebView.setWebViewClient(webViewClient);
        mWebView.loadUrl(fileName);
    }

    private void readAssetImg(a) {
        Bitmap bitmap = null;
        try {
            InputStream is = getAssets().open("img/ic_test.png");
            bitmap = BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bitmap ! =null){ mImgShow.setImageBitmap(bitmap); }}private void readAssetTxt(a) {
        String result = "";
        try {
            InputStream is = getAssets().open("word.txt");
            int lenght = is.available();
            byte[]  buffer = new byte[lenght];
            is.read(buffer);
            result = new String(buffer, "utf8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTv.setText("word.txt:\n"+result); }}Copy the code

activity_main.xml


      
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
      
        <Button
            android:id="@+id/btn_read_assets_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="read_assets_txt"
            />
        <Button
            android:id="@+id/btn_read_assets_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="read_assets_img"
            />
        <Button
            android:id="@+id/btn_read_assets_h5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="read_assets_h5"
            />
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content:"
            />
        <ImageView
            android:id="@+id/img_show"
            android:layout_width="80dp"
            android:layout_height="80dp"/>
        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code