AssetManager

AssetManager用于获取assets下的资源。

1、getassets()得到AssetManager 
2、AssetManager.close() 关闭AssetManager 
3、Resources和Assets 中的文件只可以读取而不能进行写的操作。 
4、AssetManager类常用方法:

返回指定路径下的所有文件及目录名:
final String[] list(String path)
使用 ACCESS_STREAMING模式打开assets下的指定文件: final InputStream open(String fileName)
使用显示的访问模式打开assets下的指定文件: final InputStream open(String fileName
, int accessMode)

访问assets下的资源:

1、访问assets下的网页:

//实例化WebView对象  
webview = new WebView(this);  
//加载需网页       
webview.loadUrl("file:///android_asset/index.html");  
setContentView(webview); 

注意:文件名字不能带有空格和其他非法字符,只能英文字母大小、数字、下划线。

2、访问图片和文本文件

 AssetManager asset=getAssets();
 InputStream in=null;
 try {
        in=asset.open("images/logo.gif");
        Bitmap  bitmap=BitmapFactory.decodeStream(in);
        imageview1.setImageBitmap(bitmap);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
   }

try {
        in=asset.open("test/test.txt");

        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
        byte[] buffer=new byte[1024];
        int byteCount=0;
        while((byteCount=in.read(buffer))!=-1){
           outSteam.write(buffer,0,byteCount);
        }
        byte[] buffer1=outSteam.toByteArray();
        String str=new String(buffer1);
        Log.i("Test", str);

   } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
   }

3、获取assets的文件及目录名:

// path为文件夹路径
String fileNames[] =context.getAssets().list(path);
原文地址:https://www.cnblogs.com/blosaa/p/9569355.html