Ionic 阅读文本文件在离子- Android 12

3pmvbmvn  于 2023-05-11  发布在  Ionic
关注(0)|答案(1)|浏览(135)

晚上好,我无法解决的问题涉及在Ionic 6和Android 12中阅读文本文件(方法readAsText)。使用FilePath、FileChooser和File库,结果存在,但我不能提取文本(返回null)。以前有人遇到过这样的问题吗?
下面的代码直到几个月前还在工作:

//PROPERTY
import { Injectable } from '@angular/core';
import { File } from '@ionic-native/file/ngx';
import { Device } from '@awesome-cordova-plugins/device/ngx';
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { FilePath } from '@awesome-cordova-plugins/file-path/ngx';
import { Platform } from '@ionic/angular';
import { HttpService } from './http.service';
import { Chooser } from '@awesome-cordova-plugins/chooser/ngx';

//CONSTRUCTOR
public file: File,
public device: Device,
private platform: Platform,
public fileChooser: FileChooser,
private chooser: Chooser,
public http: HttpService,
private filePath: FilePath 

....

//SNIPPET WITH ERROR
this.fileChooser
          .open()
          .then((uri) => {
            this.filePath
              .resolveNativePath(uri)
              .then((url) => {
                this.file
                  .resolveLocalFilesystemUrl(url)
                  .then((fileEntry: any) => {
                    this.platform.ready().then(() => {
                      debugger;
                      this.file.checkFile(fileEntry['nativeURL'].replace(fileEntry['name'], ''), fileEntry['name']).then(response => {
                        debugger;
                        if (response === true) {
                          this.file
                            .readAsText(
                              fileEntry['nativeURL'].replace(fileEntry['name'], ''),
                              fileEntry['name']
                            )
                            .then((result) => {
                              if (result) {
                           ---->     //RESULT ONLY NULL <---------
                                debugger;
                                resolve(result);
                              } else {
                                reject('Errore');
                              }
                            })
                            .catch((err) => {
                              console.log('err-->' + JSON.stringify(err));
                            });
                        }
                      }).catch(err => {
                        debugger;
                      });
                    });
                  });
              })
              .catch((err_3) => {
                reject(err_3);
              });
          })
          .catch((err_2) => {
            reject(err_2);
          });
arknldoa

arknldoa1#

我写我做了什么,以防有人需要。要允许系统读取图像、视频和音频以外的文件,必须使用Android存储访问框架(SAF)。在Ionic上,它与cordova-plugin-saf-mediastore插件集成。希望对你有用,再见

相关问题