how can i navigate trhoug file in my device and open it in ionic app? I follow this 2 tutorial both work but in the first one i can’t open file and i don’t know how to insert the second one to open file. Please help
FIRST,
SECOND.
My code
var app=angular.module(‘starter’, [‘ionic’,‘ngCordova’])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});
app.controller("ExampleController", function($scope, $cordovaFile, $ionicPlatform, $fileFactory, $cordovaPrinter, $cordovaFileOpener2, $interval, $rootScope) {
$scope.openPDF= function() {
$cordovaFileOpener2.open(
fileSystem.name,
'application/pdf'
).then(function() {
console.log('Success');
}, function(err) {
console.log('An error occurred: ' + JSON.stringify(err));
});
};
var fs = new $fileFactory();
$ionicPlatform.ready(function() {
fs.getEntriesAtRoot().then(function(result) {
console.log("result "+ JSON.stringify(result));
$scope.files = result;
}, function(error) {
console.error(error);
});
$scope.getContents = function(path) {
fs.getEntries(path).then(function(result) {
console.log("result "+JSON.stringify(result));
// console.log("result "+result);
$scope.files = result;
$scope.files.unshift({name: "[parent]"});
//funzione per trovare il padre del path corrente
fs.getParentDirectory(path).then(function(result) {
// console.log("result "+result);
console.log("result "+JSON.stringify(result));
result.name = "[parent]";
$scope.files[0] = result;
/*
$cordovaFileOpener2.open(
fileSystem.fullPath,
'application/pdf'
).then(function() {
console.log('Success');
}, function(err) {
console.log('An error occurred: ' + JSON.stringify(err));
});
deferred.resolve(entries);
console.log("entries "+ JSON.stringify(entries));
*/
});
}, function(error){
console.error(error);
});
}
});
});
app.factory("$fileFactory", function($q, $cordovaFileOpener2) {
var File = function() {};
File.prototype = {
getParentDirectory: function(path) {
//questa è la promise
var deferred = $q.defer();
//accedo al file local e prendo il path
window.resolveLocalFileSystemURL(path, function(fileSystem) {
fileSystem.getParent(function(result) {
console.log("result "+JSON.stringify(result));
deferred.resolve(result);
}, function(error) {
deferred.reject(error);
});
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
},
//per trovare tutti i file e cartelle nella root del device
getEntriesAtRoot: function() {
var deferred = $q.defer();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
console.log(fileSystem.name);
var directoryReader = fileSystem.root.createReader();
directoryReader.readEntries(function(entries) {
// console.log("entries "+entries);
console.log("result "+ JSON.stringify(entries));
deferred.resolve(entries);
}, function(error) {
deferred.reject(error);
});
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
},
//per trovare tutti i files e cartelle nel path dato
getEntries: function(path) {
var deferred = $q.defer();
window.resolveLocalFileSystemURL(path, function(fileSystem) {
console.log(fileSystem.name);
console.log(fileSystem.fullPath);
var directoryReader = fileSystem.createReader();
console.log("directoryReader "+JSON.stringify(directoryReader));
directoryReader.readEntries(function(entries) {
// console.log("entries "+entries);
/*
$cordovaFileOpener2.open(
fileSystem.fullPath,
'application/pdf'
).then(function() {
console.log('Success');
}, function(err) {
console.log('An error occurred: ' + JSON.stringify(err));
});
deferred.resolve(entries);
console.log("entries "+ JSON.stringify(entries));
*/
}, function(error) {
deferred.reject(error);
});
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
};
return File;
});