Hi all, I know this is not an Ionic query at all really, but the help I’ve had from here has been great, so I am trying my luck.
I would like to be able to copy a file from the file system, using the operating system default file selector, into the storage folder of an app (well, a sub directory “Documents”).
I’ve installed the following plugins and am testing with Android
cordova plugin add GitHub - don/cordova-filechooser: Cordova Plugin that supplies a File Chooser
cordova plugin add org.apache.cordova.file
When I run the supplied code (with a button in the view with the code ng-click=“copyImage”) I get the following console log that indicates that the file has actually been copied
download complete: data/data/com.ionicframework.localstorage2811988/Documents2/filename
However, when using a file browser, I don’t seem to be able to find said file!
Can anyone give me any tips?
As always, thank you. The code follows:
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var exampleApp = angular.module('starter', ['ionic'])
.run(function($ionicPlatform,$rootScope) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});
exampleApp.controller('MainCtrl',function($scope){
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
console.log("got filesystem");
var entry=fileSystem.root;
console.log(cordova.file.applicationStorageDirectory+"Documents2");
entry.getDirectory(cordova.file.applicationStorageDirectory+"Documents2", {create: true, exclusive: false}, function onGetDirectorySuccess(){ console.log("Created directory") }, function(e){ console.log("error"); console.log(e)})
}
function downloadImage(url, fileName){
var ft = new FileTransfer();
ft.download(
url,
cordova.file.applicationStorageDirectory+"Documents2/" + fileName,
function(entry) {
console.log(entry);
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("error");
console.log(error);
console.log("download error" + error.code);
}
);
}
$scope.copyImage = function(){
fileChooser.open(
function fcSuccess(URI){
downloadImage(URI,"filename");
},
function fcError(e){
console.log(e);
}
)
};
});