Соответствующий AndroidManifest.xml
Код: Выделить всё
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
Код: Выделить всё
public async openFile(): Promise {
this.pageloader.show();
const data: Blob = await firstValueFrom(this.myService.GetFile(params));
try {
let filePath = '';
if (isAndroid) {
filePath = this.writeFile_Android(data, doc.name);
} else {
filePath = this.writeFile_iOS(data, doc.name);
}
if (filePath) {
setTimeout(() => {
this.openFileFromPath(filePath);
this.pageloader.hide();
}, 500);
} else {
this.pageloader.hide();
}
} catch (e) {
console.log('openFile error');
this.pageloader.hide();
console.error(e);
}
}
private writeFile_Android(fileData: Blob, fileName: string): string {
try {
fileName = fileName.replace(/\//g, '-');
fileName = fileName.replace(/ /g, '-');
fileName = fileName.replace(/:/g, '-');
const folder = Folder.fromPath(
android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS
).getAbsolutePath()
);
const blob = (Array as any).create('byte', fileData.size);
for (let i = 0; i < blob.length; i++) {
blob[i] = (fileData as any)._buffer[i];
}
let docFile: fileSystem.File = folder.getFile(fileName);
docFile.writeSync(blob);
return docFile.path;
} catch (e) {
console.log('writeFile_Android error');
console.error(e, e.stack);
return null;
}
}
// import { Utils } from '@nativescript/core';
private openFileFromPath(path: string) {
console.log(`path: ${path}`);
// content://com.[company].[product].local.provider/external/Download/Text-Document.txt
try {
let opened = Utils.openFile(path);
if (!opened) {
console.error(`download file not opened '${path}'`);
}
} catch (e) {
console.log('openFileFromPath error');
console.error(e, e.stack);
}
}
Код: Выделить всё
Angular CLI: 16.2.16
Node: 18.15.0
Package Manager: npm 9.5.0
OS: win32 x64
Angular: 14.3.0
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1402.13
@angular-devkit/build-angular 14.2.13
@angular-devkit/core 14.2.13
@angular-devkit/schematics 16.2.16 (cli-only)
@ngtools/webpack 14.2.13
@schematics/angular 16.2.16 (cli-only)
rxjs 7.8.1
typescript 4.8.4
zone.js 0.12.0
Подробнее здесь: https://stackoverflow.com/questions/790 ... ndroid-app
Мобильная версия