我正在使用 MultipartEntity,并且尝试引用原始文件夹中的文件。这是代码:
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart(new FormBodyPart("file", new FileBody(new File("test.txt"))));
test.txt 文件位于我的 res/raw 文件夹中。当我执行代码时,出现以下异常: FileNotFoundException:/test.txt:打开失败:ENOENT(没有此类文件或目录)
谁能帮我这个?
不幸的是你不能创建一个File
直接从原始文件夹中获取对象。您需要将其复制到 SD 卡或应用程序的缓存中。
您可以通过这种方式检索文件的InputStream
InputStream in = getResources().openRawResource(R.raw.yourfile);
try {
int count = 0;
byte[] bytes = new byte[32768];
StringBuilder builder = new StringBuilder();
while ( (count = in.read(bytes,0, 32768)) > 0) {
builder.append(new String(bytes, 0, count));
}
in.close();
reqEntity.addPart(new FormBodyPart("file", new StringBody(builder.toString())));
} catch (IOException e) {
e.printStackTrace();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)