028-Android存储(3):assets文件详解

694 阅读1分钟

【代码是最好的老师】

1.概述

  1. assets目录用于存储app的原始文件
  2. assets目录下所有文件不会被编译,会被原样打包进APK
  3. 使用特定工具类AssetManager读取Assets目录下文件
  4. 通常存放1-文本;2-不压缩图像;3-h5混合开发相关文件;4-音视频文件
  5. 不同于res/raw,assets下文件不能使用R.xx.id引用
方法功能
list(String path)获取path下的文件和文件夹名称
open(String fileName)打开文件,默认ACCESS_STREAMING模式
open(String fileName, int accessMode)指定打开模式。有
未指定:ACCESS_UNKNOWN
随机:ACCESS_RANDOM
顺序:ACCESS_STREAMING
缓存:ACCESS_BUFFER
close()关闭回收资源

2.使用

加载文本.txt

加载图片

加载h5

2.1加载文本

 private void readAssetTxt() {
        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);
    }

2.2加载图片


    private void readAssetImg() {
        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);
        }
    }

2.1加载html


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

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

3.注意

  1. 尽量避免存放大文件,会直接影响apk包体积
  2. 建议混合开发情形下,才使用assets存放文件
  3. 需要直接引用的情形,也可使用/res/raw原始文件的方式

4.其他代码

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() {
        String fileName = "file:///android_asset/index.html";
        mTv.setText(fileName);

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

    private void readAssetImg() {
        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() {
        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);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="内容:"
            />
        <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>