我知道这是基本的但是卡住了。我给出了像这样的文件路径
String path = "file:///android_asset/xls/x.xlsx";
File f = new File(path);
我收到Filenotfound异常和路径显示 file:/android_asset/xls/x.xlsx 这是错误的,因为我想" file:/// android_asset / XLS / x.xlsx" 即可。有帮助吗?感谢
答案 0 :(得分:2)
你可以尝试类似的东西
InputStream is = getResources().getAssets().open("x.xlsx");
如果您的文件在assets文件夹中有内部文件夹,请尝试这种方式
AssetManager am = getAssets();
InputStream is = am.open(file:///android_asset/xls/x.xlsx);
或尝试这样
InputStream is = getResources().getAssets().open("xls/x.xlsx");
File file = createFileFromInputStream(is);
如果你想要文件对象,那么使用这个
final File createFileFromInputStream(InputStream is);
try{
File f=new File(my_file_name);
OutputStream outputStream=new FileOutputStream(f);
byte buffer[]=new byte[1024];
int length=0;
while((length=inputStream.read(buffer))>0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
}catch (IOException e){
//Logging exception
}