自己创建的项目第一次可以,更新assets下的文件后 第二次写入不行
- 发布:2020-09-21 15:51
- 更新:2020-09-23 10:57
- 阅读:587
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win10
HBuilderX类型: 正式
HBuilderX版本号: 2.8.10
手机系统: Android
手机系统版本号: Android 7.0
手机厂商: 华为
手机机型: huawei
页面类型: vue
打包方式: 离线
项目创建方式: HBuilderX
操作步骤:
预期结果:
能使用android原生最基本的东西
能使用android原生最基本的东西
实际结果:
无法写入偶发性
无法写入偶发性
bug描述:
首先有2个项目:
① 是从官方下载的,改写成我自己的demo,没有问题。
② 是自己创建的项目,在使用assets下的文件并且写入到android的手机缓存中出现的问题。
请问为什么会出现,这样的差距,2种方式均进行了基座的打包更新,官方对这个有限制吗,还是说需要配置什么地方,没有看到相关文档说明
这是写入的代码,也是android最原生的方法,我不知道为什么这样的方法在你们的项目中会有这么多问题.
public static void initLicenseFile(Context context, String licenseId) {
//写入到内置存储应用包名缓存目录下
File targetFile = new File(context.getCacheDir(), licenseId);
//如果这个授权文件不存在,或者已存在需要更新时,将授权文件写入手机目录
if (targetFile.exists()) targetFile.delete();
InputStream myInput = null;
OutputStream myOutput = null;
try {
myInput = context.getAssets().open(licenseId);
myOutput = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (myInput!=null&&myOutput!=null) {
myOutput.flush();
myOutput.close();
myInput.close();
}
} catch (IOException e) {
e.fillInStackTrace();
}
}
}
Gavin2020 (作者)
原因找到了,在uni项目里的话,执行这段代码会出io异常也就是读写的时候失败,第一、读取assets下文件失败, 第二、写入文件失败,不知道为啥,好好的原生代码,到你们这就不兼容,麻烦找下原因(估计几个月后才有人解决吧,@官方根本不理),呵呵