android 对于asset和raw下文件的操作

android 对于asset和raw下文件的操作

Android 中资源分为两种,一种是res下可编译的资源文件, 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,访问也很简单,只需要调用R.XXX.id即可;第二种就是放在assets文件夹下面的原生资源文件,放在这个文件夹下面的文件不会被R文件编译,所以不能像第一种那样直接使用.Android提供了一个工具类,方便我们操作获取assets文件下的文件:AssetManager

介绍函数
String[] list(String path);//列出该目录下的下级文件和文件夹名称
InputStream open(String fileName);//以顺序读取模式打开文件,默认模式为ACCESS_STREAMING
 
 InputStream open(String fileName, int accessMode);//以指定模式打开文件。读取模式有以下几种:
                                    //ACCESS_UNKNOWN : 未指定具体的读取模式
                                    //ACCESS_RANDOM : 随机读取
                                    //ACCESS_STREAMING : 顺序读取
                                    //ACCESS_BUFFER : 缓存读取
void close()//关闭AssetManager实例
加载assets目录下的网页
webView.loadUrl("file:///android_asset/html/index.htmll");

说明:这种方式可以加载assets目录下的网页,并且与网页有关的css,js,图片等文件也会的加载。

加载assets目录下的图片资源
InputStream is = getAssets().open(fileName);  
bitmap = BitmapFactory.decodeStream(is); 
ivImg.setImageBitmap(bitmap);
加载assets目录下文本文件
InputStream is = getAssets().open(fileName);  
int lenght = is.available();  
byte[]  buffer = new byte[lenght];  
is.read(buffer);  
String result = = new String(buffer, "utf8");
加载assets目录下音乐
// 打开指定音乐文件,获取assets目录下指定文件的AssetFileDescriptor对象  
AssetFileDescriptor afd = am.openFd(music);  
mPlayer.reset();  
// 使用MediaPlayer加载指定的声音文件。  
mPlayer.setDataSource(afd.getFileDescriptor(),  
    afd.getStartOffset(), afd.getLength());  
// 准备声音  
mPlayer.prepare();  
// 播放  
mPlayer.start();  
加载assets目录下视频
private void initview() {
         vv = (CustomVideoView) view.findViewById(R.id.videoView111);
         //vv.setVideoPath("/mnt/hd/Wonder Girls - Nobody.avi");
         Uri uri = copyFile("one.3gp");
         vv.setVideoURI(uri); 
         vv.start();
     }
     
  public Uri copyFile(String name) {
         try {
             File dir = getActivity().getFilesDir();
             File file = new File(dir, name);
             if (file.exists()) {
                 Log.d("Test", "=========file exist=========");
                 return Uri.fromFile(file);
             } else {
                 file.createNewFile();
                 OutputStream os = new FileOutputStream(file);
                 InputStream is = getActivity().getAssets().open(name);
                 byte[] buffer = new byte[1024];
                 int bufferRead = 0;
                 while((bufferRead = is.read(buffer)) != -1) {
                     os.write(buffer, 0, bufferRead);
                 }
                 os.flush();
                 is.close();
                 os.close();
                 Log.d("Test", "=========copyFile success=========");
                 return Uri.fromFile(file);
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return null;
     }
获取raw目录下的资源 返回流和文件路径
/**
 * 获取raw目录下的资源
 *
 * @param resId 资源id
 */
public static InputStream getRawStream(Context context, int resId) {
    return context.getResources().openRawResource(resId);

}
/**
 * 获取raw目录下的资源
 *
 * @param resId 资源id
 */
public static String getRawFilePath(Context context, int resId) {
    return "android.resource://" + context.getPackageName() + "/" + resId;
}

拷贝assets中文件到指定路径
/**  

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

以下是完整的工具类代码 直接拷贝到工程中就可以使用
package com.insworks.lib_datas.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值