AssetManager用于获取assets下的资源。html
一、getassets()获得AssetManager
二、AssetManager.close() 关闭AssetManager
三、Resources和Assets 中的文件只能够读取而不能进行写的操做。
四、AssetManager类经常使用方法:android
返回指定路径下的全部文件及目录名: final String[] list(String path)
使用 ACCESS_STREAMING模式打开assets下的指定文件: final InputStream open(String fileName)
使用显示的访问模式打开assets下的指定文件: final InputStream open(String fileName, int accessMode)
访问assets下的资源:web
一、访问assets下的网页:this
//实例化WebView对象 webview = new WebView(this); //加载需网页 webview.loadUrl("file:///android_asset/index.html"); setContentView(webview);
注意:文件名字不能带有空格和其余非法字符,只能英文字母大小、数字、下划线。spa
二、访问图片和文本文件code
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(); }
三、获取assets的文件及目录名:htm
// path为文件夹路径 String fileNames[] =context.getAssets().list(path);