Pre-Knowledge:
Android Resource File Categories:
Android resource files can be broadly divided into two types:
The first is a compiled resource file that is stored in the Res directory:
This resource file system will automatically generate the ID of the resource file in R.java, so access to this resource file is relatively simple, through the r.xxx.id can be;
The second is a native resource file stored under the assets directory:
Because the system does not compile the resource files under assets, we cannot access them in r.xxx.id way. Can I access them through the absolute path of the resource? Because APK installation will be placed in the/data/app/**.apk directory, in the form of APK, asset/res and is bound to the APK, and will not be extracted into the/data/data/yourapp directory down, So we can't get the absolute path to the assets directly, because they don't have it at all.
Okay Android The system provides us with a Assetmanager Tool class.
Viewing the official API, Assetmanager provides access to the application's original resource file; This class provides a low-level API that allows you to open and read the raw resource files that are bound together with the application as a simple byte stream.
Assetmanager class Overview:
Provides access to the application's original resource file; This class provides a low-level API that allows you to open and read the raw resource files that are bound together with the application in the form of simple byte streams. Gets the Assetmanager object through the Getassets () method.
Common methods of the Assetmanager class:
Public Methods |
| final string[] |
List ( STRING  path) |
| final inputstream |
open ( STRING  filename) use access_streaming mode open assets . |
| Final InputStream |
Open (String fileName, int accessmode) open using the displayed access mode Assets the specified file under . |
Application Examples 1. Load the Web page in the assets directory:
// Loading assets/win8_demo/ under the directory index.html Web page
WebView. Loadurl ("file:///android_asset/win8_Demo/index.html");
Description: This method can be loaded Assets The pages in the directory, and the CSS , JS , pictures and other files will also be loaded.
2. Access the resource files under the assets directory:
Assetmanager.open (String filename), which returns a byte stream of type Inputsteam, where filename must be a file such as (aa.txt;img/semll.jpg) and not a folder.
3. Get the file and directory name of the assets:
// Get Assets all files and directory names under the directory, content (the current context, such as Activity , Service wait Contextwrapper can be used for the child class)
String filenames[] =context.getassets (). List (path);
4. Copy the files under assets to the SD card:
/** * Copy entire folder contents from the Assets Directory * @param context context uses activity of the CopyFiles class * @param oldpath String original file path such as:/AA * @param NewPath String After copying the path such as: XX:/BB/CC */public void Copyfilesfassets (Context context,string oldpath,string NEWPA Th) {try {String filenames[] = context.getassets (). List (OldPath);//Get all the files and items in the assets directory Record name if (Filenames.length > 0) {//If the directory is file file = new file (NewPath); File.mkdirs ();//If the folder does not exist, recursion for (String filename:filenames) {copyfilesfassets (Context,oldpath + "/" + filename,newpath+ "/" +filename); }} else {//If it is a file InputStream is = Context.getassets (). open (OldPath); FileOutputStream fos = new FileOutputStream (new File (NewPath)); byte[] buffer = new byte[1024]; int bytecount=0; while ((Bytecount=is.read (buffer))!=-1) {//Loop reads buffer bytes from the input stream fos.write (buffer, 0, ByteCount);//writes the read input stream to the output stream} fos.flush ();//Flush buffer is.close (); Fos.close (); }} catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); Notifies the UI thread MainActivity.handler.sendEmptyMessage (copy_false) If an error is caught; } }
5. Use the image resource under the assets directory:
InputStream is=getassets (). Open ("wpics/0zr424l-0.jpg"); Bitmap Bitmap=bitmapfactory.decodestream (IS); Imgshow.setimagebitmap (BITMAP);