Android项目使用Assets下的文件

浏览:
字体:
发布时间:2013-12-17 09:37:38
来源:

Android项目在编译时,Assets下文件不被编译。

Assets下的文件除了 html文件可以直接在项目中使用外,其他的文件都需要做处理滴。

在项目中使用方法:
使用流读取。
AssetManager manager = getAssets();

InputStream open = manager.open("logo.png");

注意:某些经常使用的文件(比如数据库a.db),可以在项目初始化时将其copy到手机中存储。示例见下边2

//示例一 Get the AssetManager

 AssetManager manager = getAssets();    // read a Bitmap from Assets    InputStream open = null;    try {      open = manager.open("logo.png");      Bitmap bitmap = BitmapFactory.decodeStream(open);      // Assign the bitmap to an ImageView in this layout      ImageView view = (ImageView) findViewById(R.id.imageView1);      view.setImageBitmap(bitmap);    } catch (IOException e) {      e.printStackTrace();    } finally {      if (open != null) {        try {          open.close();        } catch (IOException e) {          e.printStackTrace();        }      }    } 

实例二
/** * 拷贝assets中的电话归属地数据库文件 */private void copyAddrDB() {	File file = new File(getFilesDir(), "address.db");	if (file.exists()) {		Log.i("log:SplashActivity", "数据库已经存在,不需要拷贝。。。");	} else {		Log.i("log:SplashActivity", "开始拷贝了。。。。");				// 拷贝		try {			InputStream is = getAssets().open("address.db");	// 获取数据库库文件输入流			FileOutputStream fos = new FileOutputStream(file);	// 定义输出流			byte[] bt = new byte[8192];			int len = -1;			while((len = is.read(bt)) != -1){				fos.write(bt, 0, len);			}			is.close();			fos.close();					} catch (IOException e) {			e.printStackTrace();		}	}}

转载请标明:大飞_Rflyee:http://blog.csdn.net/rflyee/article/details/17341669


>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();