my ionic3我爱编程

Cordova在Android系统下的文件操作要点

2018-02-24  本文已影响49人  环零弦

昨天涉及到操作 SQLCipherAndroid 下的加解密操作,最初的需求是新建一个未加密的 Sqlite 数据库文件,然后分发到目标 Android 系统后,再根据实际情况加密(可能是根据特定目标平台的某具体参数设置加密密钥)。而根据昨天的调研结果,SQLCipher 中如果是加密的数据库文件,可以简单地更改密码,但如果是“加密数据库文件”与“平文数据库文件”之间的转换,则必须重新建一个文件,然后导数据。

于是,随之而来的问题便是 怎样在目标平台中新建文件。嗯,

因为穷,

买不起 Mac,所以没办法在苹果系设备中做开发以及测试,目前先只考虑安卓。

具体步骤:

1. 添加 Cordova 的文件系统操作插件

cordova plugin add cordova-plugin-file`

2. 具体代码

import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';
declare let cordova: any;
declare let window: any;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage implements OnInit {
  ngOnInit() {
    document.addEventListener('deviceready', () => {
      window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, root => {
        alert(JSON.stringify(root));
        root.getFile('demo.txt', { create: true }, fileEntry => {
          alert(JSON.stringify(fileEntry));
        }, error => {
          alert(JSON.stringify(error))
        });
      }, error => {
        alert(JSON.stringify(error))
      });
    });
  }
}

这里网上有个坑,cordova.file.applicationStorageDirectory,这个东西是不能加引号的。这个字段的具体表意如下:

Android File System Layout: (From https://www.npmjs.com/package/cordova-plugin-file#android-file-system-layout)

Device Path cordova.file.* AndroidExtraFileSystems r/w? persistent? OS clears private
file:///android_asset/ applicationDirectory assets r N/A N/A Yes
/data/data/<app-id>/ applicationStorageDirectory - r/w N/A N/A Yes
cache cacheDirectory cache r/w Yes Yes* Yes
files dataDirectory files r/w Yes No Yes
Documents documents r/w Yes No Yes
<sdcard>/ externalRootDirectory sdcard r/w Yes No No
Android/data/<app-id>/ externalApplicationStorageDirectory - r/w Yes No No
cache externalCacheDirectory cache-external r/w Yes No** No
files externalDataDirectory files-external r/w Yes No No

* The OS may periodically clear this directory, but do not rely on this behavior. Clear the contents of this directory as appropriate for your application. Should a user purge the cache manually, the contents of this directory are removed.
** The OS does not clear this directory automatically; you are responsible for managing the contents yourself. Should the user purge the cache manually, the contents of the directory are removed.
Note: If external storage can't be mounted, the cordova.file.external* properties are null.

注意事项

参考资料:

上一篇下一篇

猜你喜欢

热点阅读