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);

4、访问assets下资源,并且复制到SD卡
代码实现:请点击

更多相关文章

  1. Android:使用SAX或者DOM或者pull操作XML文件
  2. android系统中运行jar文件
  3. Android frameworks添加资源后编译报错:找不到添加的内部资源 com
  4. 使用android MediaPlayer播放音频文件时,有时会出现prepareasync
  5. Android Studio NDK及so文件开发 以及常见错误
  6. Android 读取sdcard指定目录文件

随机推荐

  1. System Server 分析
  2. android 上调试动态库方法
  3. Android(安卓)kernel 编译
  4. android:shape的使用
  5. Android应用程序的数据存放目录 路径
  6. Android(安卓)Gradle Plugin指南(一)——简
  7. 我对android的第一印象
  8. GridLayout 网格布局
  9. android用DroidDraw实现可视化UI编程
  10. Android笔记--handler机制