Read and write about the files in the assets directory in Android _android

Source: Internet
Author: User

The Android resource files can be roughly divided into two categories:

The first is a compiled resource file stored in the Res directory:

This resource file system will automatically generate the ID of the resource file in R.java, so it is simpler to access this resource file, through R.xxx.id;

The second is the native resource files stored in the assets directory:

Because the system does not compile assets resource files at compile time, we cannot access them in r.xxx.id way. Can I access them through the absolute path of the resource? Because APK will be placed in the/DATA/APP/**.APK directory after installation, in the form of APK, Asset/res and is bound in the APK, and will not extract into the/data/data/yourapp directory down, So we can't get the absolute path to the assets directly, because they don't.

Luckily the Android system provides us with a Assetmanager tool class.

Viewing the official API, Assetmanager provides access to the original resource files of the application; This class provides a low-level API that allows you to open and read the original resource files that are bound to the application in a simple byte stream.

In addition to providing/res directory storage resource files, Android also provides storage resources in the/assets directory, The assets directory does not automatically generate IDs in R.java, so reading the resource files under the assets directory requires a path that we can access through the Assetmanager class.
The author needs to implement a copy of resources from a.apk (resource apk, all resources such as: So, apk, executables, etc. below assets directory, APK does not implement logical code) to the specified directory, so the author creates a b.apk that implements the logic of resource copying (a service, can also be implemented by activity), because the copy path is generally inaccessible or created (only after each APK installation can access/data/data/'s own package name/the following private space), the author needs this apk to get system privileges, You must shareduserid the Androidmanifest.xml declaration, and how to do the following section to record.


First, Assetmanager read files commonly used in several APIs

1. File read mode
Assetmanager.open (String filename), returns a inputsteam type of byte stream, where the filename must be a file, not a folder, Assetmanager The Open method of opening a resource file is an overloaded method that can add an int parameter that is open and can be manipulated depending on the parameter. Please see the Official document Http://web.mit.edu/clio/MacData/afs/sipb/project/android/docs/reference/android/content/res/AssetManager.html
2. Resource files can exist folders and subdirectories
Public final string[]list (String path) returns the names of all files and subdirectories below the current directory. You can iterate through the entire file directory recursively to achieve access to all resource files. String[] Array of strings, one for each asset. These file names are relative to ' path '. You can open the file by concatenating ' path ' and a name in the returned string (via file) and passing "to open" ().

Second, the relevant implementation code
resource apk (a.apk)

Implementation of the code fragment, due to the use of system permissions, the generated path can be changed b.apk

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    try {ctxdealfile = This.createpackagecontext ("Com.zlc.ipanel", context.context_ignore_security);
    catch (Namenotfoundexception E1) {//TODO auto-generated catch block E1.printstacktrace (); Btn3.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//T
          Odo auto-generated Method Stub try {String uifilename = "Ipaneljoin";
        Deepfile (Ctxdealfile, uifilename);
          catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
        Textview.settext ("File is wrong");
    }
      }
    }); public void Deepfile (context Ctxdealfile, string path) {try {string str[] = Ctxdealfile.getassets (). L
      IST (path); if (Str.length > 0) {//if directory file = new file ("/data/"+ path);
        File.mkdirs ();
          for (String string:str) {path = path + '/' + String;
          System.out.println ("zhoulc:\t" + path);
          Textview.settext (Textview.gettext () + "T" +path+ "T");
          Deepfile (ctxdealfile, path);
        Path = path.substring (0, Path.lastindexof ('/'));
        } else {//If it is a file InputStream is = Ctxdealfile.getassets (). open (path);
        FileOutputStream fos = new FileOutputStream (New File ("/data/" + path));
        byte[] buffer = new byte[1024];
        int count = 0;
          while (true) {count++;
          int len = is.read (buffer);
          if (len = = 1) {break;
        } fos.write (buffer, 0, Len);
        } is.close ();
      Fos.close ();
    The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();

 }
  }

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.