首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ionic 3中访问sdcard文件

如何在Ionic 3中访问sdcard文件
EN

Stack Overflow用户
提问于 2017-09-11 10:53:13
回答 2查看 2.8K关注 0票数 2

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

代码语言:javascript
复制
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');

 }

}

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

代码语言:javascript
复制
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));
}

}

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

代码语言:javascript
复制
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));
}
EN

回答 2

Stack Overflow用户

发布于 2017-09-11 11:04:09

您可以通过使用Native plugin file轻松地做到这一点。

此插件实现了files,允许对驻留在设备上的文件进行读/写访问。 File类实现了访问文件和目录的静态方便函数。

代码语言:javascript
复制
 ionic cordova plugin add cordova-plugin-file
 npm install --save @ionic-native/file

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

Android:外部存储(SD卡)根。

请看Git repo here

票数 0
EN

Stack Overflow用户

发布于 2017-11-12 20:59:30

这可能有助于:

代码语言:javascript
复制
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);
});

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46153968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档