京东上苹果官网买手机邮寄的寄修玻璃更换是啥意思

android assets文件夹资源的访问
来源:博客园
1、assets文件夹里面的文件都是保持原始的文件格式 。
2、assets中的文件只可以读取而不能进行写的操作。
3、assets目录下的资源文件不会在R.java自动生成ID,所以读取assets目录下的文件必须指定文件的路径。
4、assets则可以有目录结构,也就是assets目录下可以再建立文件夹。
 
5、读取assets 目录下的文件

AssetManager assetManager = getAssets() ;
InputStream inputStream = assetManager.open( "fileName" ) ;

 

InputStream is = getResources().getAssets().open( "aa.txt" ) ;

 
6、读取assets 目录下的文本

String s = getAssetsString( "aa.txt" ) ;

/**
* 读取本地文件中字符串
* @param fileName
* @return
private String getAssetsString(String fileName) {
StringBuilder stringBuilder = new StringBuilder();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(
getAssets().open(fileName), "UTF-8") );
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
} catch (IOException e) {
e.printStackTrace();
return stringBuilder.toString();
}

 
7、读取assets 目录下的图片

Bitmap bgImg = getImageFromAssetFile( "background.png" );
* 从assets中读取图片
private Bitmap getImageFromAssetsFile(String fileName)
Bitmap image = null;
AssetManager am = getResources().getAssets();
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
catch (IOException e)
e.printStackTrace();
}

免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动1195人阅读
android(3)
我们在做android开发的时候,有时候会需要把asset目录的文件复制到SD卡里,然后直接从SD卡里面读取文件,资源文件放到SD卡里面后读取起来就很方便,不像在asset文件夹里面必须通过文件流的方式进行读取,在这里我把我用到的asset复制到SD卡目录的代码分享一下,希望能够帮助有需要的小伙伴,代码如下。。。
* 复制asset文件到指定目录
asset下的路径
SD卡下保存路径
public static void CopyAssets(Context context, String oldPath, String newPath) {
String fileNames[] = context.getAssets().list(oldPath);
if (fileNames.length & 0) {
File file = new File(newPath);
file.mkdirs();
for (String fileName : fileNames) {
CopyAssets(context, oldPath + "/" + fileName, newPath + "/" + fileName);
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) {
fos.write(buffer, 0, byteCount);
fos.flush();
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
该方法需要传入3个参数,第一个参数为上下文对象,第二个参数为文件在asset文件夹下面的路径,第三个参数是复制到SD卡里面的路径,使用方法想当简单,有兴趣的小伙伴可以试试看。。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:21520次
排名:千里之外
原创:33篇
评论:11条
(1)(1)(1)(1)(6)(1)(1)(2)(1)(1)(2)(3)(3)(5)(8)Jhuster 的BLOG
用户名:Jhuster
文章数:158
评论数:396
访问量:1334326
注册日期:
阅读量:5863
阅读量:12276
阅读量:379212
阅读量:1071717
51CTO推荐博文
我们知道,Android工程目录下有一个assets文件夹,它到底有什么作用呢?网上关于assets文件夹与res/raw文件夹的区别的讨论挺多的,也没有一个统一官方的结论,到底何时用assets,何时用res/raw,这里我也来谈谈我的理解和总结。我认为raw目录毕竟是存放于res目录下的,因此,应该更多地把raw目录看作是系统资源文件的一部分。res/raw目录下的文件会被映射到R.java文件中,访问的时候可以直接使用资源ID,例如:R.id.filename,并且很多Android的API都支持传入这样的id参数,因此我的结论是:以“资源”的形式在Android程序中引用的文件,可以放到res/raw目录下,如声音片段,html文件,图片文件等等。而assets该目录可以有多层次的目录结构,里面的文件不会被映射到R.java中,访问的时候需要使用AssetManager类,一般以文件流(InputStream)的形式来打开和访问,因此,我的结论是:该目录下一般存放一些“非资源”类型的文件,多半是自定义或者结构复杂的文件,比如:游戏/应用的初始配置文件、数据库文件、字典数据、字体文件等等。其实,在实际的应用中,assets目录的文件,大都是作为应用的初始化配置,在应用初始安装启动后,拷贝到系统的sdcard中存储的。Android系统在sdcard上为每一个应用分配了存储路径:/sdcard/Android/data/$(应用的包路径),该路径可以通过 context.getExternalFilesDir 得到,一般应用卸载的时候,该目录也会随之被删除。关于assets目录与res/raw目录的区别就介绍到这,下面我给出一份我封装好的类,提供将assets目录下的文件按目录结构拷贝到sdcard的实现。package&com.ticktick.
import&java.io.BufferedR
import&java.io.F
import&java.io.FileOutputS
import&java.io.IOE
import&java.io.InputS
import&java.io.InputStreamR
import&java.io.OutputS
import&java.util.ArrayL
import&java.util.L
import&android.content.C
import&android.content.res.AssetM
&*&&AssetCopyer类
&*&&实现将assets下的文件按目录结构拷贝到sdcard中
&*&&@author&ticktick
&*&&@Email&lujun.
public&class&AssetCopyer&{
private&static&final&String&ASSET_LIST_FILENAME&=&"assets.lst";
private&final&Context&mC
private&final&AssetManager&mAssetM
private&File&mAppD
public&AssetCopyer(&Context&context&)&{
&&&&&&&&&&&&mContext&=&
&&&&&&&&&&&&mAssetManager&=&context.getAssets();
&*&&将assets目录下指定的文件拷贝到sdcard中
&*&&@return&是否拷贝成功,true&成功;false&失败
&*&&@throws&IOException
public&boolean&copy()&throws&IOException&{
&&&&List&String&&srcFiles&=&new&ArrayList&String&();
&&&&//获取系统在SDCard中为app分配的目录,eg:/sdcard/Android/data/$(app's&package)
&&&&//该目录存放app相关的各种文件(如cache,配置文件等),unstall&app后该目录也会随之删除
&&&&mAppDirectory&=&mContext.getExternalFilesDir(null);
&&&&&&&&&&&&if&(null&==&mAppDirectory)&{
&&&&&&&&&&&&&&&&return&
&&&&&&&&&&&&}
&&&&&&&&&&&&//读取assets/$(subDirectory)目录下的assets.lst文件,得到需要copy的文件列表
&&&&&&&&&&&&List&String&&assets&=&getAssetsList();&
&&&&&&&&&&&&for(&String&asset&:&assets&)&{&&&&&&&&
&&&&&&&& //如果不存在,则添加到copy列表
&&&&&&&& if(&!&new&File(mAppDirectory,asset).exists()&)&{
srcFiles.add(asset);
&&&&&&&& }
&&&&&&&&&&&&}&&&&&&&& &&
&&&&&&&&&&&&//依次拷贝到App的安装目录下
&&&&&&&&&&&&for(&String&file&:&srcFiles&)&{
&&&&&&&&&&&&&&&&copy(file);
&&&&&&&&&&&&}
&&&&return&
&*&&获取需要拷贝的文件列表(记录在assets/assets.lst文件中)
&*&&@return&文件列表
&*&&@throws&IOException
protected&List&String&&getAssetsList()&throws&IOException&{
&&&&List&String&&files&=&new&ArrayList&String&();
&&&&InputStream&listFile&=&mAssetManager.open(new&File(ASSET_LIST_FILENAME).getPath());
&&&&BufferedReader&br&=&new&BufferedReader(new&InputStreamReader(listFile));
&&&&String&
&&&&&&&&&&&&while&(null&!=&(path&=&br.readLine()))&{
&&&&&&&& files.add(path);
&&&& &&&&}
&&&&&&&&&&&&return&
&*&&执行拷贝任务
&*&&@param&asset&需要拷贝的assets文件路径
&*&&@return&拷贝成功后的目标文件句柄
&*&&@throws&IOException
protected&File&copy(&String&asset&)&throws&IOException&{
&&&&&&&&InputStream&source&=&mAssetManager.open(new&File(asset).getPath());
&&&&&&&&File&destinationFile&=&new&File(mAppDirectory,&asset);
&&&&&&&&destinationFile.getParentFile().mkdirs();
&&&&&&&&OutputStream&destination&=&new&FileOutputStream(destinationFile);
&&&&&&&&byte[]&buffer&=&new&byte[1024];
&&&&&&&&int&
&&&&&&&&while&((nread&=&source.read(buffer))&!=&-1)&{
&&&&&&&&&&&&if&(nread&==&0)&{
&&&&&&&&&&&&&&&&nread&=&source.read();
&&&&&&&&&&&&&&&&if&(nread&&&0)
&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&destination.write(nread);
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&}
&&&&&&&&&&&&destination.write(buffer,&0,&nread);
&&&&&&&&destination.close();
&&&&&&&&return&destinationF
}注意,本拷贝代码的实现要求assets目录下必须有一个assets.lst文件,列出需要被拷贝到sdcard的文件列表。附件中是示例工程的代码,工程结构如图所示:& &&650) this.width=650;" src="/wyfs02/M00/28/D6/wKioL1N7anfxs0uNAAFdhM6accY130.jpg" title="assets.png" alt="wKioL1N7anfxs0uNAAFdhM6accY130.jpg" />& &&其中,assets.lst 文件内容如下:&&&map/china.txt
&&&map/france.txt& &&& & 示例工程中,执行了AssetCopyer.copy之后(注意放到线程中执行),会发现assets目录下的文件被拷贝到了系统的/sdcard/Android/data/testassets/files目录下了。& & 关于Android的assets的拷贝就介绍到这儿了,有任何疑问欢迎留言探讨,或者来信lujun.交流,或者关注我的新浪微博 获取最新的文章和资讯。本文出自 “” 博客,请务必保留此出处
了这篇文章
附件下载:  
类别:┆阅读(0)┆评论(0)
请输入验证码:顶级域名生效咯! 转载原创文章请注明出处,谢谢!
> Android中拷贝Assets目录下的文件至SD卡中
权限配置:
&uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/&
&uses-permission android:name=”android.permission.MOUNT_UNMOUNT_FILESYSTEMS”/&
具体代码:
String path = Environment.getExternalStorageDirectory().getPath();//SD卡路径
path += &/pengmj&;
InputStream is = getApplicationContext().getAssets().open(&zww.png&);//读取assets目录下的zww.png文件
BufferedInputStream bis = new BufferedInputStream(is);
byte[] temp = new byte[1024];
int size = 0;
File file = new File(path+&/zww.png&);
FileOutputStream fos = new FileOutputStream(file);
while((size=bis.read(temp))&-1){
if(size&0){
fos.write(temp, 0, size);
fos.flush();
fos.close();
Log.i(&A&, path);
catch(Exception e){
e.printStackTrace();
转载请注明: &

我要回帖

更多关于 买手机去京东还是苏宁 的文章

 

随机推荐