Android assents

来源:互联网 发布:淘宝买水果拒收 编辑:程序博客网 时间:2024/06/10 11:44

预前知识:

Android资源文件分类:


Android资源文件大致可以分为两种:

第一种是res目录下存放的可编译的资源文件:

    这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问这种资源文件比较简单,通过R.XXX.ID即可;

 第二种是assets目录下存放的原生资源文件:

      因为系统在编译的时候不会编译assets下的资源文件,所以我们不能通过R.XXX.ID的方式访问它们。那我么能不能通过该资源的绝对路径去访问它们呢?因为apk安装之后会放在/data/app/**.apk目录下,以apk形式存在,asset/res和被绑定在apk里,并不会解压到/data/data/YourApp目录下去,所以我们无法直接获取到assets的绝对路径,因为它们根本就没有。

还好Android系统为我们提供了一个AssetManager工具类。

      查看官方API可知,AssetManager提供对应用程序的原始资源文件进行访问;这个类提供了一个低级别的API,它允许你以简单的字节流的形式打开和读取和应用程序绑定在一起的原始资源文件。

AssetManager类

通过getAssets()方法获取AssetManager对象。

Public Methods

final String[]

list(String path)

返回指定路径下的所有文件及目录名。

final InputStream

open(String fileName)

使用 ACCESS_STREAMING模式打开assets下的指定文件。.

final InputStream

open(String fileName, int accessMode)

使用显示的访问模式打开assets下的指定文件.

1.加载assets目录下的网页:

webView.loadUrl("file:///android_asset/win8_Demo/index.html");

2.访问assets目录下的资源文件:

 AssetManager.open(String filename),返回的是一个InputSteam类型的字节流,这里的filename必须是文件比如
(aa.txt;img/semll.jpg),而不能是文件夹。

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

//获取assets目录下的所有文件及目录名,content(当前的上下文如ActivityServiceContextWrapper的子类的

都可以)

String fileNames[] =context.getAssets().list(path);    

4.将assets下的文件(包括这个目录下的文件和文件夹)复制到SD卡:

/**  *  从assets目录中复制整个文件夹内容  *  @param  context  Context 使用CopyFiles类的Activity *  @param  oldPath  String  原文件路径  如:/aa  *  @param  newPath  String  复制后路径  如:xx:/bb/cc  */ public void copyFilesFassets(Context context,String oldPath,String newPath) {                             try {        String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名        if (fileNames.length > 0) {//如果是目录            File file = new File(newPath);            file.mkdirs();//如果文件夹不存在,则递归            for (String fileName : fileNames) {               copyFilesFassets(context,oldPath + "/" + fileName,newPath+"/"+fileName);            }        } else {//如果是文件            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) {//循环从输入流读取 buffer字节                        fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流            }            fos.flush();//刷新缓冲区            is.close();            fos.close();        }    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();        //如果捕捉到错误则通知UI线程                   MainActivity.handler.sendEmptyMessage(COPY_FALSE);    }                           }        

5.将assets下的文件复制到SD卡:

private void copyBigDataToSD(String strOutFileName,String assentname) throws IOException     {          InputStream myInput;          OutputStream myOutput = new FileOutputStream(strOutFileName);          myInput = this.getAssets().open(assentname);          byte[] buffer = new byte[1024];          int length = myInput.read(buffer);        while(length > 0)        {            myOutput.write(buffer, 0, length);             length = myInput.read(buffer);        }                myOutput.flush();          myInput.close();          myOutput.close();            }

6.使用assets目录下的图片资源:

InputStream is=getAssets().open("wpics/0ZR424L-0.jpg");
Bitmap bitmap=BitmapFactory.decodeStream(is);
imgShow.setImageBitmap(bitmap);

7.播放assets目录下的音乐

首先,获取通过openFd()的方法获取asset目录下指定文件的AssetFileDescriptor对象

其次,通过MediaPlayer对象的setDataSource (FileDescriptorfd, longoffset, long length)方法加载音乐文件。

最后,调用prepare方法准备音乐,start方法开始播放音乐。

预备知识:

AssetFileDescriptor简介:

        在AssetManager中一项的文件描述符。这提供你自己打开的FileDescriptor可用于读取的数据,以及在文件中的

偏移量和长度的该项的数据。

可以通过AssetManageropenFd()的方法获取asset目录下指定文件的AssetFileDescriptor对象。

 常用方法:

Public Methods

FileInputStream

createInputStream()

为asset创建并返回一个自动关闭的输入流。

FileOutputStream

createOutputStream()

为asset创建并返回一个自动关闭的输出流。

FileDescriptor

getFileDescriptor()

返回可用于读取文件中的数据的FileDescriptor对象。

long

getLength()

返回该asset中项的数据的总字节数。

long

getStartOffset()

返回asset中项的数据字节开始偏移。

具体代码:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. // 打开指定音乐文件,获取assets目录下指定文件的AssetFileDescriptor对象  
  2. AssetFileDescriptor afd = am.openFd(music);  
  3. mPlayer.reset();  
  4. // 使用MediaPlayer加载指定的声音文件。  
  5. mPlayer.setDataSource(afd.getFileDescriptor(),  
  6.     afd.getStartOffset(), afd.getLength());  
  7. // 准备声音  
  8. mPlayer.prepare();  
  9. // 播放  
  10. mPlayer.start();  


0 0
原创粉丝点击