Android 如何访问爱奥尼亚3中的SD卡文件

Android 如何访问爱奥尼亚3中的SD卡文件,android,angular,typescript,cordova-plugins,ionic3,Android,Angular,Typescript,Cordova Plugins,Ionic3,我在爱奥尼亚3上工作,我是爱奥尼亚的新手,所以我尝试从我使用的设备和android存储访问权限上传文档,但我只能访问设备的内部存储,我想从sd卡访问文件。 下面是我的代码 import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { RemoteServiceProvider} from "../../provider

我在爱奥尼亚3上工作,我是爱奥尼亚的新手,所以我尝试从我使用的设备和android存储访问权限上传文档,但我只能访问设备的内部存储,我想从sd卡访问文件。 下面是我的代码

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RemoteServiceProvider} from "../../providers/remote-service/remote-service";
import { AndroidPermissions } from '@ionic-native/android-permissions';

@IonicPage()
@Component({
 selector: 'page-home',
 templateUrl: 'home.html',
 })
export class HomePage {

 android: AndroidPermissions;
 constructor(public remoteServiceProvider: RemoteServiceProvider, public 
 navParams: NavParams, public nvCtr: NavController) {
//this.getStates();
this.android= new AndroidPermissions();
this.android.requestPermissions([this.android.PERMISSION.READ_EXTERNAL_STORAGE,this.android.PERMISSION.WRITE_EXTERNAL_STORAGE])


  }

  getStates(){
this.remoteServiceProvider.getResult().subscribe(data => {
  alert(data._body);
},error => {
  alert(error);
});

 }
 ionViewDidLoad() {
  console.log('ionViewDidLoad HomePage');

 }

}

上面的代码是我授予用户权限的地方,下面是我访问文件的地方:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FileChooser } from '@ionic-native/file-chooser';
import { FilePath } from '@ionic-native/file-path';
import { File } from '@ionic-native/file';
import {Platform} from "ionic-angular";
@IonicPage()
@Component({
 selector: 'page-document',
  templateUrl: 'document.html',
})

export class DocumentPage {
fileChooser: FileChooser;
filePath: FilePath;
nativepath: any;
file: File;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.fileChooser= new FileChooser();
this.filePath= new FilePath();
this.file=new File();
}

ionViewDidLoad() {
  console.log('ionViewDidLoad DocumentPage');
}

openFile(){
this.fileChooser.open()
.then(uri => this.convertFilePath(uri))
.catch(e => alert(e));
}

convertFilePath(filePathUri: string){
this.filePath.resolveNativePath(filePathUri)
.then(filePath =>{
  this.nativepath= filePath;
  //Here in nativepath we get filepath
})
.catch(err => alert("filePath "+err));
}
openFile(){
   this.fileChooser.open()
   .then(uri => this.convertFilePath(uri))
   .catch(e => alert(e));
}

convertFilePath(filePathUri: string){
 this.filePath.resolveNativePath(filePathUri)
 .then(filePath =>{
  this.nativepath= filePath;
  //Here in navtivepath i get the file path
 })
 .catch(err => alert("filePath "+err));
}
}



上面的代码是我授予用户权限的地方,下面是我访问文件的地方:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FileChooser } from '@ionic-native/file-chooser';
import { FilePath } from '@ionic-native/file-path';
import { File } from '@ionic-native/file';
import {Platform} from "ionic-angular";
@IonicPage()
@Component({
 selector: 'page-document',
  templateUrl: 'document.html',
})

export class DocumentPage {
fileChooser: FileChooser;
filePath: FilePath;
nativepath: any;
file: File;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.fileChooser= new FileChooser();
this.filePath= new FilePath();
this.file=new File();
}

ionViewDidLoad() {
  console.log('ionViewDidLoad DocumentPage');
}

openFile(){
this.fileChooser.open()
.then(uri => this.convertFilePath(uri))
.catch(e => alert(e));
}

convertFilePath(filePathUri: string){
this.filePath.resolveNativePath(filePathUri)
.then(filePath =>{
  this.nativepath= filePath;
  //Here in nativepath we get filepath
})
.catch(err => alert("filePath "+err));
}
openFile(){
   this.fileChooser.open()
   .then(uri => this.convertFilePath(uri))
   .catch(e => alert(e));
}

convertFilePath(filePathUri: string){
 this.filePath.resolveNativePath(filePathUri)
 .then(filePath =>{
  this.nativepath= filePath;
  //Here in navtivepath i get the file path
 })
 .catch(err => alert("filePath "+err));
}

通过使用,您可以轻松做到这一点

这个插件实现了一个文件API,允许对文件进行读/写访问 驻留在设备上

File类实现了访问文件的静态便利函数 和目录

您需要在Android上使用
externalRootDirectory
实例成员

Android:外部存储(SD卡)根目录

请参见。

这可能有助于:

import { File } from "@ionic-native/file";
import { Diagnostic } from "@ionic-native/diagnostic";

constructor(
    ...
    public file: File,
    public diagnostic: Diagnostic
){

this.diagnostic.getExternalSdCardDetails()
.then( (data) => {
  this.jcError += "\n" + "sd:" + JSON.stringify( data);
  this.jcError += "\n" + "Number cards: " + data.length;
  for( let ii = 0; ii < data.length; ii += 1){
    let thisElem = data[ii];
    if( thisElem.type.toLowerCase() === "application" && thisElem.canWrite){
      this.ourSDentry = thisElem;
      basePathSD = thisElem.filePath;
      break;
    }
  }
  if( !basePathSD){
    this.jcError += "\n\n" + "no SD card found";
    return;
  }
}, (errData)=>{
  tag = "getExternalSdCardDetails";
  this.jcError += "\n\n" + tag + ":ERR:" + JSON.stringify( errData);
});
从“@ionic native/File”导入{File};
从“@ionic native/Diagnostic”导入{Diagnostic}”;
建造师(
...
公共文件:文件,
公共诊断:诊断
){
this.diagnostic.getExternalSdCardDetails()
。然后((数据)=>{
this.jcerro+=“\n”+“sd:+JSON.stringify(数据);
this.jcerro+=“\n”+”数字卡:“+data.length;
对于(设ii=0;ii{
tag=“getExternalSdCardDetails”;
this.jcerro+=“\n\n”+标记+”:ERR:“+JSON.stringify(errData);
});

然后,您可以使用basePathSD访问SD卡。

您好,谢谢您的回复,我有代码可以读取文件,但无法访问文件。我可以授予存储权限,但它仍然无法访问SD卡文件。您可以显示该代码吗?您显示了其他内容否?上面的代码是我授予使用权限的地方我访问文件的位置是:openFile(){this.fileChooser.open().then(uri=>this.convertFilePath(uri)).catch(e=>alert(e));}convertFilePath(filePathUri:string){this.filePath.ResolvePath(filePathUri)).then(filePath=>{this.nativepath=filePath;//在navtivepath中我得到了文件路径}).catch(err=>alert(“filePath”+err));}你有控制台错误吗?没有,没有错误。。