Android 시스템에서 Cordova의 파일 작업 포인트

4397 단어
어제 SQLcipher가 안드로이드에서 복호화 작업을 하는 것과 관련된 작업은 암호화되지 않은 Sqlite 데이터베이스 파일을 새로 만들어서 목표 안드로이드 시스템에 나누어 준 다음에 실제 상황에 따라 암호화하는 것이 최초의 요구였다. (특정 목표 플랫폼의 특정한 파라미터에 따라 암호화 키를 설정할 수도 있다.)어제 조사 결과에 따르면 SQLcipher에서 암호화된 데이터베이스 파일이라면 암호를 간단하게 변경할 수 있지만'암호화된 데이터베이스 파일'과'평문 데이터베이스 파일'사이의 변환이라면 파일을 다시 만들고 데이터를 이끌어야 한다.
따라서 대상 플랫폼에서 파일을 새로 만드는 방법이 문제입니다.응,
가난하기 때문에,
맥을 살 수 없어서 애플 기기에서 개발 및 테스트를 할 수가 없어요. 지금은 안드로이드만 생각하고 있어요.
구체적인 단계:
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//
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
/
externalRootDirectory
sdcard
r/w
Yes
No
No
Android/data//
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.
주의 사항
  • 파일 시스템 권한 문제에 대해 저는 권한을 신청하지 않은 상황에서 응용 프로그램 StorageDirectory(/data/data///)와 external RootDirectory(/)에서 파일을 만드는 데 성공했기 때문에 에서 말한 것처럼 현저한 정의 권한이 필요하지 않습니다.요청 권한이 필요한 경우 다음과 같이 씁니다. In config.xml:
    
      ...
      
        ...
        
    ...
    ...
    
  • 무지하고 구체적인 조사 연구를 하지 않았기 때문에 어떤 안드로이드의 파일 시스템에는/data/data 경로가 전혀 없다. 그러나 대부분의 안드로이드 시스템은 안드로이드/data 디렉터리를 가지고 있기 때문에 external Application Storage Directory라는 파라미터를 쓰는 것이 비교적 안전하다고 제안한다.
  • 또 하나 언급해야 할 것은 ts 파일에서 상기 코드의 window와cordova와 같은 변수를 어떻게 사용하는지 파일 앞에서 다음과 같이 설명해야 한다는 것이다.
    declare let cordova: any;
    declare let window: any;
    
    이상, 에서 상당히 그다지 좋지 않은 설명이 있다.

  • 참조 자료:
  • Github: cordova-plugin-file#android-file-system-layout
  • NPM: cordova-plugin-file
  • Cordova는 파일 생성 및 읽기/쓰기 가능(Android 및 IOS 지원 등)
  • Typescript Error: Cannot find name 'cordova'
  • Ionic 2 : Use NFC (How to define PERMISSION in Android)
  • 안드로이드 다양한 액세스 권한Permission 상세 정보
  • 좋은 웹페이지 즐겨찾기