FIle copyTo not working in cordova - javascript

In my cordova(v 3.3) application i used copyTo for copy files from one directory to another directory in sdcard. Some time it is working fine some time it is not working.
Here is the source i used to copy files from one dierctory to another.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) {
var root = fileSystem.root.fullPath;
console.log(skiTemplateFileName);
alert(root);
var fails = function(err) { alert(err.code);}
window.resolveLocalFileSystemURI(root+"/"+TEMPLATE_DIRECTORY+""+TemplateFileName, function(file) {
window.resolveLocalFileSystemURI(root+"/"+PDF_DIRECTORY, function(destination) {
file.copyTo(destination,randomFileName);
alert("File Download Successfully");
},fails)
},fails);
}, function fail(e){
});
when copy files first time only shows the file download success. For next time it shows only root alert
Note : This issue occurs in samsung tab 4(v4.4.2) devices working well in nexus 7.
Please guide to solve this issue.

Related

How do I use a downloaded svg file within my application using Tizen Web studio?

I can get an SVG file downloaded, additionally, I can display svg files as you would normally within an image tag. I do not know how to access the folder location for downloads or the wgt-private folder so I may download images to a client's watch and then use the downloaded version.
I'm sure my file is downloading as I've console logged on successful download and when I list the items in the directory the file shows up.
Placing downloads/[filename] or wgt-private/[filename] does not appear to work as these are virtual file locations however I've no idea how to access these files within the application without using the filesystem methods.
Download:
var download_obj = new tizen.DownloadRequest('someFile.svg', 'wgt-private');//Hidden the actual location however this file does display when enterting the whole file location
tizen.download.start(download_obj, {
onprogress: function(id, receivedSize, totalSize) {
console.log(id);
console.log(receivedSize);
console.log(totalSize);
},
onpaused: function(id) {
console.log(id);
},
oncanceled: function(id) {
console.log(id);
},
oncompleted: function(id, fullPath) {
console.log(id);
console.log(fullPath);
},
onfailed: function(id, error) {
console.log(id);
console.log(JSON.stringify(error));
}
});
Full path comes out as: wgt-private/someFile.svg
Doesn't display as displays a file error in the console on all attempts.
I understand that your questions relates to how to show the image downloaded with tizen.download API in html img tag.
I can see two workarounds that could help you with it:
You can use filesystem API (which you would like to avoid), BUT since 5.0 there is a method which needs no additional privileges and I hope it will match your needs - FileSystemManager.toURI(). It just gets the path to file (returned by download API) and returns the full URI, able to be used in img.
I noticed that download to non-public directories on the device, download API returns the 'hidden' path which uses virtual root, but when downloading to public directory as 'downloads', the full path is returned and it works for img as well.
If both of above is not acceptable for you, I am afraid that the only alternative is to use regular tizen.filesystem API and resolve the path from download API and then use File.toURI() function to get the path.
var link = "http://techslides.com/demos/samples/sample.jpg"
var download_obj = new tizen.DownloadRequest(link, 'wgt-private');//Hidden the actual location however this file does display when enterting the whole file location
tizen.download.start(download_obj, {
oncompleted: function(id, fullPath) {
console.log("completed " + id + " : " + fullPath);
tizen.filesystem.resolve(fullPath, (s)=>{console.log("Resovled full path: " + s.toURI())}, (e) => {console.log(e)})
},
onfailed: function(id, error) {
console.log("failed " + id);
console.log(JSON.stringify(error));
}
});
You can find the proper web sample app: new Tizen project - Sample - Mobile 4.0 - Web application - Content - Download Manager
Open index.html and replace https://www.sample-videos.com/video/mkv/720/big_buck_bunny_720p_10mb.mkv with your file address.

Saving files in Phonegap

I've been running through docs and everything for the past couple of hours but with no success...
I want to download a file from some server and to save it on my phone (the directory where the app stores files that are only visible to my app and are deleted when the app is uninstalled). I would like to store a file and write to it (I've done the download part). With the following code, when I run it, it works and display the text from the "file" but when I search for the file using some file explorer apps, the file that I've "created" is nowhere near to be found.
Cheers!
EDIT Notes:
Okay so I've now even tried to create a file on my phone from the computer using a usb connection so I know for a fact the the file exists...
The path of the file where I created it from the pc is:
This PC\Galaxy S7 edge\Phone\Android\data\com.phonegap.musicapp2\text.txt
The response code that I get from JSON.stringify(dirEntry) is the following
{"isFile":false,"isDirectory":true,"name":"com.phonegap.musicapp2","fillPath":"/data/user/0/com.phonegap.musicapp2/", "filesystem":"","nativeURL":"file:///data/user/0/com.phonegap.musiapp2/"}
So when I run the code from bellow, it gives me
Error from getFile: {"code":1}
I've looked through the docs and it says that code 1 mean: NOT_FOUND_ERR (http://prntscr.com/n071kx)
"Code from bellow"
function readFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
DebugConsole.log("Successful file read: " + this.result);
// displayFileData(fileEntry.fullPath + ": " + this.result);
};
reader.readAsText(file);
}, onErrorReadFile);
}
window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, function(dirEntry) {
DebugConsole.log(JSON.stringify(dirEntry));
dirEntry.getFile('text.txt', {create: false, exclusive: false}, function(fileEntry) {
//writeFile(fileEntry, null, isAppend);
readFile(fileEntry);
}, function(e) {
DebugConsole.log('Error from getFile: ' + JSON.stringify(e));
});
});
Sidenote:
App never asks for permission to access my internal storage.
I went into Apps/myapp/permissions and it showed that the app doesn't have permission to access the storage. I turned it on but still no luck.
I think I need to somehow request storage access from the app but I can't seem to find a way how?
I managed to find a solution with the following
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + "Android/data/app.name/files/", success, fail);

Phonegap (Cordova) - File not found Exception in reading file from FileSystem

In order to send one file from my android device's File System to one server, I am using cordova 3.5 and the following code (as you can get reading this code the file is inside my Download directory):
receivedEvent: function(id) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fail);
}
function fsSuccess(fs){
fs.root.getDirectory("Download", {create: false, exclusive: false}, doDirectoryListing, FileError);
}
function doDirectoryListing(dirEntry) {
var directoryReader = dirEntry.createReader();
directoryReader.readEntries(gotFiles, FileError);
}
function gotFiles(entries)
{
var wsUrl = "http://myServer/";
var fileToSend = "file:///storage/sdcard0/Download/"+entries[0].name, "file:///storage/sdcard0/Download/";
multipart.uploadFiles(success, error, wsUrl, fileToSend);
}
I can read all the files (names) that are in /Download parsing all the entries but I dont know how to get a specific file (for instance entries[0]) which needs to be passed to uploadFiles function.
Problem: I tried in different manner as for instance concatenating it with "file:///storage/sdcard0/Download/" but always getting a file not found exception.
I found the solution on my own,
mainly file:// is to be removed.
It could be gained this way: fileToSend.replace("file://", ""));

Phonegap File API not returning correct path only returns '/'

I have been developing a phonegap+angularjs app and building for android and iphone using phonegap builder. When I use the File API of phonegap, I cant access the app documents folder in IOS and not able to move or copy my file from one location to another based on the documents folder. After taking a picture, I tried to get the fullpath of the image to move it to persistent folder like this
window.resolveLocalFileSystemURI(fileURL, function(fileEntry) {
});
But fileEntry returns '/' only instead of 'file://var/mobile/......' in iOS.
Is this an issue with phonegap? It was working fine 2 days earlier but not now
Please help
I think phonegap change it to resolveLocalFileSystemURL (instead of ...URI) a few days ago.
From the phonegap update: http://cordova.apache.org/news/2014/02/10/plugins-release.html
CB-5008: Rename resolveLocalFileSystemURI to resolveLocalFileSystemURL
They also change how you get the full URL.
Before
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.fullPath;
},
function (evt) {
console.warn("error");
}
);
Now
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
fileSystem.root.toURL();
},
function (evt) {
console.warn("error");
}
);
Hope it helps

PhoneGap: media.startRecord() not working in cordova-1.6.1

I'm trying to record a sample audio with phoneGap, but with no success.
What I do is pretty simple, according to the documentation:
<script>
var media = null;
function record(){
media = new Media("audio.wav");
media.startRecord();
}
</script>
The "audio.wav" exists in my "www" folder and is an empty wav file.
Whenever this code block runs, I get this console error:
- ERROR: Method 'create:withDict:' not defined in Plugin 'Media'
- FAILED pluginJSON = {"className":"Media","methodName":"create","arguments":["INVALID","952d1fe0-5ec7-5e48-d68a-74cc979878b5","audio.wav"]}
Even with that error, the recording supposedly continues. But when I try to play the recording, I watch the media object at the debugger and I see '_duration : -1'
A different error appears when I try to debug the application on an actuall iPhone device.
Actually, the problem was that even thought I correctly created the file on the root directory as you depict, I couldn't access it later on, even though I got it's URI (File not found for some reason, on the same directory the file was created a second ago!!!!).
So I found out (not the easy way..) that I have to access it via "documents://". So when I create a new Media I do something like :
var mediaFile = new Media ("documents://"+audioFileName).
If I had done "var mediaFile = new Media(fileURI_I_Just_Created) It would say that file does not exist!!!! (gotta love phoneGap!)
You need to create a blank wav file in the root directory, not the www folder. One of the ways you can get the root directories URI is by using the method toURI().
This is what the URI will look like:
file:///data/some/com.stack.overflow/
Below is an example of how you can create a blank wav file in the root directory:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("blank.wav", {create: true, exclusive: false}, gotFileEntry, fail);
}
Note:
create: Used to indicate that the file or directory should be created, if it does not exist. (boolean)
exclusive: By itself, exclusive has no effect. Used with create, it causes the file or directory creation to fail if the target path
already exists. (boolean)

Categories

Resources