如何读取文件夹资产android系统中的文件? [英] How to read a file from assets folder in android?

查看:147
本文介绍了如何读取文件夹资产android系统中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成功我写程序来读取资源文件夹一个文件并将其分配给文本视图。
现在,我想读的所有文件,并将其分配给文本视图,将你的任何一个可以帮助我该怎么办?所有的文件是文本文件,感谢你提前。

Successfully i wrote program to read single file in asset folder and assign it to text view. Now i want to read all files and assign it to the text view, will any one of you help me how to do? all the files are text files, thankful to you in advance.

推荐答案

****在Android中,我们无法从资产文件夹文件u必须从asseset文件复制到SD卡不是执行阅读搜索结果**

****In Android we can't read file from assets folder u have to copy file from asseset to sdcard than perform reading

**

编辑:这种说法是错误的。见注释。

this statement is wrong. See comments.

以下code使用了从资产文件夹搜索结果进行拷贝
私人无效copyAssets(){结果
    AssetManager assetManager = getAssets();结果
    的String []文件= NULL;结果
    尝试{结果
        文件= assetManager.list();结果
    }赶上(IOException异常五){结果
        Log.e(标签,无法获得资产的文件列表。,E);结果
    }结果
    对于(字符串文件名:文件){结果
        在的InputStream = NULL;结果
        出的OutputStream = NULL;结果
        尝试{结果
          在= assetManager.open(文件名);结果
          OUT =新的FileOutputStream(/ SD卡/+文件名);结果
          copyFile(IN,OUT);结果
          in.close();结果
          在= NULL;结果
          了out.flush();结果
          out.close();结果
          出= NULL;结果
        }赶上(IOException异常五){结果
            Log.e(标签,无法复制资源文件:+文件名,E);结果
        }结果
    }结果
}搜索结果
私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException搜索结果
    字节[]缓冲区=新的字节[1024]结果
    INT读;结果
    而((读= in.read(缓冲))!= -1){结果
      out.write(缓冲,0,读);结果
    }结果
}结果**

use following code for perform copy from assets folder

private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}

private void copyFile(InputStream in, OutputStream out) throws IOException {

byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
**

这篇关于如何读取文件夹资产android系统中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆