`

android中加载assets中的资源文件

 
阅读更多


1.直接读取inputstream流

 

  1. AssetManager assetManager = getAssets();//獲取其輸入流 然後直接讀取這個流  
  2. InputStream inputStream = assetManager.open("1.txt");    

 

 

2.複製asset指定文件到制定地方

 

  1. /** 
  2.      * Copy assets util 
  3.      * @param path The dictionary path 
  4.      * @param filename  The filename 
  5.      * @param context  Your context 
  6.      */  
  7.     public void copyAssets(String path, String filename,Context context) {  
  8.         try {  
  9.   
  10.             File fpath = new File(PATH);  
  11.             if (!fpath.isDirectory()) {  
  12.                 fpath.mkdirs();  
  13.             }  
  14.             AssetManager assetManager = context.getAssets();  
  15.             InputStream inputStream = assetManager.open(filename);  
  16.             FileOutputStream fileOutputStream = new FileOutputStream(PATH  
  17.                     + filename);  
  18.             byte[] buffer = new byte[1024];  
  19.             int read;  
  20.             while ((read = inputStream.read(buffer)) != -1) {  
  21.                 fileOutputStream.write(buffer, 0, read);  
  22.             }  
  23.             inputStream.close();  
  24.             inputStream = null;  
  25.             fileOutputStream.flush();  
  26.             fileOutputStream.close();  
  27.             fileOutputStream = null;  
  28.   
  29.         } catch (Exception e) {  
  30.             // TODO: handle exception  
  31.         }  
  32.     }  

3.複製assets下所有文件到指定地方

 

 

  1. private static void copyAssets(Context context) {//copy Assets的方法    
  2.             
  3.             AssetManager assetManager = context.getAssets();    
  4.             String[] files = null;    
  5.             try {    
  6.                 files = assetManager.list("");    
  7.             } catch (IOException e) {    
  8.         
  9.             }    
  10.             for (int i = 0; i < files.length; i++) {    
  11.                 InputStream in = null;    
  12.                 OutputStream out = null;    
  13.                 try {    
  14.                     if (!(new File(Tips.DATA_PATH + files[i])).exists()) {    
  15.                         in = assetManager.open(files[i]);    
  16.                         out = new FileOutputStream(Tips.DATA_PATH + files[i]);    
  17.                         Tips.copyFile(in, out);    
  18.                         in.close();    
  19.                         in = null;    
  20.                         out.flush();    
  21.                         out.close();    
  22.                         out = null;    
  23.                     }    
  24.                 } catch (Exception e) {    
  25.                 }    
  26.             }    
  27.         }    
  28.         
  29.         public static void copyFile(InputStream in, OutputStream out)    
  30.                 throws IOException {    
  31.             byte[] buffer = new byte[1024];//做了个缓冲流    
  32.             int read;    
  33.             while ((read = in.read(buffer)) != -1) {    
  34.                 out.write(buffer, 0, read);    
  35.             }    
  36.         }    
分享到:
评论

相关推荐

    Android 加载assets中的资源文件实例代码

    主要介绍了Android 加载assets中的资源文件实例代码的相关资料,这里附有实例代码,需要的朋友可以参考下

    Android播放assets文件里视频文件相关问题分析

    因为之前写webview加载assets文件夹时,是这样写的: webView = new WebView(this); webView.loadUrl(file:///android_asset/sample3_8.html); 依次类推,我尝试将视频video.3gp 放到assets目录下,然后写了下面的...

    afinal0.5.1框架 支持android下assets文件夹下图片加载

    现要加载assets目录下资源是的数据构造为 String[] data ={"assets://iting/1.jpg","assets://iting/2.jpg"}(assets://后面加的是图片相对于assets的路径) 其他用法为原版用法,仅更新此功能。 我的博客地址 ...

    android动态加载小栗子

    android动态加载小栗子,很简单的一个小栗子,将assets中的资源文件拷贝到file文件中,然后通过反射动态加载资源。

    AndroidStudio 实现加载字体资源的方法

    首先在androidstudio中找到assets文件夹 , (位于serc/main/java下,与res在同一个文件里面) , 找到之后将字体文件(*.ttf)放入这个文件夹下(当然为了规范起见,我们可以再asseets下建立一个font文件夹表示存放字体文件)...

    使用Glide在Android应用中加载网络图片

    使用Glide在Android应用中加载网络图片 代码由于宽度比较小被换行了,看着很不整齐,其实很整齐,注释写得比较详细,比较多不容易阅读,可以先复制到ide或者Vscode里阅读 布局文件里只有一个imageview 动画资源文件...

    关于Android应用程序外部文件引用

    Qt 利用了这种机制,实现了 assets 虚拟文件系统,我们常见的 QFile 、 QPixmap 、 QImage 等,都可以通过 "assets:/" 的 ... 使用 assets 虚拟文件系统,你的资源文件不会在程序启动时就加载到内存,比较节省资源。

    Android代码-字体文件压缩神器

    大大压缩了apk包的大小,还可以减小资源加载的占用。有此需要的同学赶紧用起来吧。支持otf、ttf、ttc、woff、eof 均测试提取成功。 sfntly(请使用FontZip) sfnttool.jar下载 sfnttool.jar源码 sfnttool.jar使用...

    Android读取properties配置文件的实例详解

    我把配置文件放在了assets文件夹下 appConfig.properties: serverUrl=http://192.168.1.155 import java.io.InputStream; import java.util.Properties; import android.content.Context; /** * 读取...

    【android开发】解析SD卡下的XML文件

    解析SD卡中的XML资源文件,获取数据,已备使用。第一次运行会提示将预留在项目中assets文件夹下xml文件拷到设备对应SD卡文件夹下,再运行就会解析出结果。我一直都用的这个,希望对大家有帮助。

    Android实现双目测距APP Demo.zip

    (2)Android Demo视频测试:项目资源(src/main/assets)自带一对左右视图的视频文件,你需要将测试视频拷贝到你的手机 OpenCV实现双目测距(Python版本):...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Android 4游戏编程入门经典

     4.1 定义一个android应用程序:清单文件  4.1.1 [manifest]元素  4.1.2 [application]元素  4.1.3 [activity]元素  4.1.4 [uses-permission]元素  4.1.5 [uses-feature]元素  4.1.6 [uses-sdk]元素  4.1.7 ...

    集成目前Android主流优秀第三方组件

    读取本地资源工具类(ToolResource.java),反射本地资源文件API,避免依赖R文件,方便jar形式发布 Android单位转换工具类(ToolUnit.java),sp/dp/px互转 自定义Toast工具类(ToolToast.java),自定义背景色、字体...

    Android开发宝典.rar

    资源和资产Resources and Assets 28 资源引用Resource Reference 43 国际化和本地化Internationalization and Localization 43 意图和意图过滤器Intents and Intent Filters 43 意图过滤器Intent filters 47 ...

    android游戏编程入门

    android游戏编程入门 《Android4游戏编程入门经典》是2012年清华大学出版社出版的图书,作者是(美)策希纳(Zechner,M.),无(美)格林(Green,R.)。 第1章 Android,后起之秀 1  1.1 Android简介 1  1.2 版本分裂 3 ...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    6.2.2 移动端工程资源布局 35 6.3 认我测在线认证检测系统的主要功能实现 36 6.3.1用户查询 36 6.3.2订单操作 37 6.3.3个人信息维护 37 6.4.系统功能测试 38 6.4.1 首页功能模块 38 6.4.2 订单功能模块 38 6.4.3 ...

    Android开发指南中文版-----应用程序框架

    资源和资产Resources and Assets 28 资源引用Resource Reference 43 国际化和本地化Internationalization and Localization 43 意图和意图过滤器Intents and Intent Filters 43 意图过滤器Intent filters 47 通常...

    Android集成主流优秀第三方组件框架

    读取本地资源工具类(ToolResource.java),反射本地资源文件API,避免依赖R文件,方便 jar形式发布 Android单位转换工具类(ToolUnit.java),sp/dp/px互转 自定义Toast工具类(ToolToast.java),自定义背景色、字体...

Global site tag (gtag.js) - Google Analytics