How to access sdcard files in Ionic 3

我是研究僧i 提交于 2019-12-11 10:26:13

问题


I am working on Ionic 3 and i am newbie in ionic so tried to upload documents from device i used and android permission for storage access but i able to access only internal storage of device, i want to access files from sd card. Here below is my code

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');

 }

}

The above code is where i grant the permissions from user and below where i access the files:

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));
}

}


The above code is where i grant the permissions from user and below where i access the files:

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));
}

回答1:


You can easily do that by using Native plugin file.

This plugin implements a File API allowing read/write access to files residing on the device.

The File class implements static convenience functions to access files and directories.

 ionic cordova plugin add cordova-plugin-file
 npm install --save @ionic-native/file

You need to use externalRootDirectory instance member for that on Android.

Android: the external storage (SD card) root.

See on Git repo here.




回答2:


This may help:

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);
});

You can then use the basePathSD to access the SD card.



来源:https://stackoverflow.com/questions/46153968/how-to-access-sdcard-files-in-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!