SoFunction
Updated on 2025-03-11

Android API programming Assets file operation example

This article describes the Android API Assets file operation. Share it for your reference, as follows:

public class ReadAssetActivity extends Activity {
  private TextView mTextView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.read_asset);
    initViews();
    readAssetsData();
    listAssetsFiles("fonts");// List file directory  }
  private void readAssetsData() {
    InputStream is = null;
    try {
      // Read the input stream of the corresponding file in the Assets folder      is = getAssets().open("asset_test.txt");
      // Get the total size of the file input stream      int size = ();
      // Window the entire file into a Byte[] buffer      byte[] buffer = new byte[size];
      (buffer);
      ();
      // Convert buffer data to string      String text = new String(buffer);
      (text);
    } catch (IOException e) {
      ();
    } finally {
      try {
        if (is != null) {
          ();
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        ();
      }
    }
  }
  private void initViews() {
    mTextView = (TextView) findViewById();
  }
  /**
    * Get the number of files in the specified folder under Assets
    *
    * @description:
    * @author ldm
    * @date 2016-4-27 9:25:54 AM
    */
  private void listAssetsFiles(String filePath) {
    AssetManager am = getAssets();
    String[] fileName;
    try {
      fileName = (filePath);
      if ( > 0) {
        for (int i = 0; i < ; i++) {
          ("ldm", ("exist" + filePath
              + "There are: [%d] files under the file path", ));
        }
      }
    } catch (IOException e) {
      ();
    }
  }
}

For more information about Android related content, please check out the topic of this site:Android file operation skills summary》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.