get iCloud file size before uploading the file through file transfer plugin.
I able to upload the file through that path but cannot find the file size.
Working in android but not in iOS
window.resolveLocalFileSystemURL(Uri,
function(fileEntry) {
fileEntry.file(function(fileObj) {
console.log("Size = " + fileObj.size);
var size = fileObj.size;
var megaSize = size / MEGABYTE;
var roundedSize = Math.round( megaSize * 10 ) / 10;
console.log("roundedSize", roundedSize);
if(roundedSize < UPLOAD_LIMIT) {
_fileUploadToServer(Uri, successCB, errorCB);
}
else {
showToast.snackbarCustom($filter('translate')('FILE_SIZE_NOT_ALLOWED'),kbUp);
}
},
function (error) {});
}, function (error) {}
in iCloud the path has /private prepended. Replace it with file:// to get the file size similarly as android
Related
I have an app that takes a photo.
I need that photo to be stored where all my assets/images/photos are.
So basically my question is :
a) how can I find the path where these assets are (on the phone)?
b) how can I add files to that same path?
Thanks
As per my knowledge you can not store image in assets package. Just because of when we build android/ios app after that assets folder is read only.
You need to store image in local mobile storage OR cloud OR catch memory
1) You can get storage path using :
Future<String> getStorageDirectory() async {
if (Platform.isAndroid) {
return (await getExternalStorageDirectory()).path; // OR return "/storage/emulated/0/Download";
} else {
return (await getApplicationDocumentsDirectory()).path;
}
}
2) Add image in path
createImage() async{
String dir= getStorageDirectory();
File directory = new File("$dir");
if (directory.exists() != true) {
directory.create();
}
File file = new File('$directory/image.jpeg');
var newFile = await file.writeAsBytes(/* image bytes*/);
await newFile.create();
}
I would like to do image annotation for my Ionic Application. So the flow of the app would be using the camera plugin to take a picture and use FabricJs to draw on the image then save the file.
I hit the roadblock when I am trying to save or overwrite the file. Apparently the source "http://localhost:8080/file/data/user/0/***/files/1547183479807.png" file does not update.
The flow of the app
1) Take picture with #ionic-native/camera
2) Copy the file to a local directory
3) Use this.win.Ionic.WebView.convertFileSrc to convert the file name to "http://localhost:8080/file/data/user/0/***/files/1547183479807.png"
4) Push to another page to access the canvas
5) Use the link to setBackground to my canvas (FabricJs)
6) Draw on the image (Manually)
7) Save the file via overwriting the existing file but nothing works from here on.
I tried to
- overwrite with writeFile & writeExisitingFile, did not work.
- removeFile and writeFile and did not work.
- tried converting to ArrayBuffer rather than Blob and did not work
- tried creating another new file, did not work too (it seems like after I push to a new page, all the file functions does not affect the files)
- tried using native cordova but did not work too.
- delete that same files twice, (first time I did not get an error but the second time I got an error saying "File Does not exist" but when I view the source, the file is also there and appearing in my thumbnail on my App.
private copyFileToLocalDir(namePath, currentName, newFileName,id,index) {
this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
const keys = id.split('-');
let filename = this.file.dataDirectory + newFileName;
this.fp = this.win.Ionic.WebView.convertFileSrc(filename) ;
this.navCtrl.push(AnnotationsPage, {
filepath: this.fp,
filename: newFileName
});
this.presentToast("Image Successfully Added",'middle');
}, error => {
this.presentToast('Error while storing file.','middle');
});
}
Annotation.ts
savePicture() {
let image = this.canvas.toDataURL({
format: 'png'
});
this.saveBase64(image);
}
public saveBase64(base64:string):Promise<string>{
return new Promise((resolve, reject)=>{
var realData = base64.split(",")[1]
let blob=this.b64toBlob(realData,"image/png")
this.file.writeFile(this.file.dataDirectory,this.filename,blob,{replace:true})
// this.file.writeExistingFile(this.file.dataDirectory,this.filename, blob)
.then((val)=>{
console.log('Write Info',val)
let fp = this.win.Ionic.WebView.convertFileSrc(this.file.dataDirectory+this.filename) ;
})
.catch((err)=>{
console.log('error writing blob')
console.log(err);
// reject(err)
})
})
}
b64toBlob(b64Data, contentType) {
contentType = contentType || '';
var sliceSize = 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
I check the base64 file, it is working fine (Throw the data to an online converter and it display the picture.
After the function "copyFileToLocalDir", it seems like I am unable to modify the files store in the local directory.
Thanks in advance. Feel free to ask me more question. 7 hours and no result.
Update on the testing, I am doing.
Did File.readAsDataUrl and as expected the file file:///data/user/0/*/files/1547231914843.png updated from the old image to the new edited. (Tested the base64 on some online converter) but it still does not reflect on the source http://localhost:8080/file/data/user/0/***/files/1547231914843.png
Thanks to all those who read through.
After tons of testing, I found out that Ionic Webview's files, will not be updated unless you recall them again (Kind of like a cache and apparently there is no way to reset or flush the cache).
So in my case, I would need to remove the image from the webview and call it out again then it will go back to retrieve again or create another file with another file name and display it.
function StartPage_imgArrow_OnTouch(e)
{
SMF.Multimedia.playSound("resources://clickOn.mp3",true,false);
}
Binding file don't work, is this path wrong?
Audio file should be in resources folder.
yourProjectName_data\resources\Sounds\
Than you can write like the code below;
function pgMap_btnSetting_OnPressed(e) {
SMF.Multimedia.playSound("clickOn.mp3",
true,false,
function(){alert("started to play...");},
function(){alert("finished...");});
}
I have sample app about Mp3 player. Make sure you have right file directory.
Here this is my script. This works on Android, I did not tried on iOS.
function Page1_Self_OnKeyPress(e) {
if (e.keyCode === 4) {
Application.exit();
}
}
var mp3path = "";
function Page1_Self_OnShow() {
//Comment following block for removing navigationbar/actionbar sample
//Copy this code block to every page onShow
header.init(this);
header.setTitle("Page1");
header.setRightItem("RItem");
header.setLeftItem();
/**/
mp3path = SMF.IO.getExternalStorages();
mp3path = mp3path[2] + "/" + "two.mp3";
alert(mp3path);
}
function Page1_TextButton1_OnPressed(e) {
SMF.Multimedia.playSound(mp3path,
true, false,
function () {
alert("started to play...");
},
function () {
alert("finished...");
});
}
function Page1_Slider1_OnChange(e){
SMF.Multimedia.setSoundLevel(Pages.Page1.Slider1.value);
}
function Page1_TextButton2_OnPressed(e){
SMF.Multimedia.stopSound();
}
mp3path is an array returned local storages on Android devices. For detailed information see this link.
I've tried to open local PDFs stored in the www/Documents folder in my XCode project. The code I put in .js file is:
Cordova.exec("ChildBrowserCommand.showWebPage", "file:///www/Documents"+pdf );
Where pdf is the name of the file, which changes for every file. It works fine on the simulator but it doesn't work on device. How can I fix this?
Thanks!
I am using below function to load files in childbrowser, it creates the correct local url in case you want to load the internal files:
function loadChildBrowser(isInternal, URL) {
if(isInternal){
var strPath = window.location.href;
var path = strPath.substr(0,strPath.lastIndexOf('/')) + URL;
cb.showWebPage(encodeURI(path));
}
else{
cb.showWebPage(URL);
}
}
Edit
Try this for your case:
function loadChildBrowser(isInternal, URL) {
if(isInternal){
var strPath = window.location.href;
var path = strPath.substr(0,strPath.lastIndexOf('/')) + URL;
Cordova.exec("ChildBrowserCommand.showWebPage", encodeURI(path) );
}
else{
Cordova.exec("ChildBrowserCommand.showWebPage", URL );
}
}
As part of an iPhone app I'm creating using PhoneGap, I need to be able to use the microphone to record to a new file which is sorted in the apps document folder on the phone.
I think I have the code sorted to actually capture the recording I'm just having trouble creating a blank .wav in the documents folder to record to. According to the PhoneGap API iOS requires that the src file for the audio already exists.
Can anyone help my with the couple of lines of code needed to create this blank file? My code so far is -
function recordAudio() {
var src = "BLANK WAV IN DOCUMENTS FOLDER";
var mediaRec = new Media(src, onSuccess, onError);
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
if (recTime >= 10) {
clearInterval(recInterval);
mediaRec.stopRecord();
}
}, 1000);
}
function onSuccess() {
console.log("recordAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
$('#record-button').
bind('tap', function(){
recordAudio();
})
You may need to create the file first using the File API.
document.addEventListener("deviceready", function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function fail(){});
}, false);
var gotFS = function (fileSystem) {
fileSystem.root.getFile("blank.wav",
{ create: true, exclusive: false }, //create if it does not exist
function success(entry) {
var src = entry.toURI();
console.log(src); //logs blank.wav's path starting with file://
},
function fail() {}
);
};
tried using something like this?
var src = "blank.wav"; instead of "BLANK WAV IN DOCUMENTS FOLDER" ?