Saving to phone then uploading to server on android

You must Login before you can answer or comment on any questions.

Hi All

| have built a recorder the saves a file and then uploads it to my server the code is below

file = recording.stop();
var newFile =Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,"/track.m4a");
newFile.write(file);
fileToUpload =Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,"/track.m4a");
var toUpload = fileToUpload.read(); 
 
var sendXhr = Titanium.Network.createHTTPClient();
    sendXhr.onload = function () { 
         if (this.status === 200) {
 
            var response = this.responseData;
 
        } else {
 
        } // ENDif return status ok    
    }
 
    sendXhr.onerror = function(e) {
 
    };
 
    sendXhr.open("POST", 'http://example.com/appupload', false);
    sendXhr.setRequestHeader('Content-Type', 'application/json');
    sendXhr.send({media: toUpload});
Ok this works fine for ios but as soon as i try with android it fails.

I have looked at the docs and see applicationDirectory is only ios so i have tried externalStorageDirectory as i see thats android but i cant seem to get this to work all i want is it to work similar to above save the file somewhere on the phone i can then upload it from there after upload i will be deleting it from the device

Where does android save a file after its been recorded???

Any help would be appreciated

Thanks

— asked 8 months ago by Ned Turner
2 Comments
  • I had the same problem. On android the media player has not the permission to play files from applicationDir. It works only if files are read from externalStorage. There is nothing you can do to change this behavior. You can just use different storage according platform, as I do.

    — commented 8 months ago by alessandro la rocca

  • hi alessandro could you please explain

    You can just use different storage according platform, as I do.

    i really want to get this working i have tried

    fileToUpload =Titanium.Filesystem.getFile(Ti.Filesystem. externalStorageDirectory,"/track.m4a");
    But this didn't work for me thanks for the help.

    — commented 8 months ago by Ned Turner

1 Answer

For Android,You have to get file from Sdcard,Use

Titanium.Filesystem.getFile(Ti.Filesystem.getExternalStorageDirectory)
and push your image in the android sdcard.

Hope it helps.

— answered 8 months ago by pankaj Goyal
answer permalink
2 Comments
  • ok thanks pankaj i will give this a try and get back to you ;)

    — commented 8 months ago by Ned Turner

  • Hi Pankaj

    I am trying to do it with the following can you tell me where i am going wrong?

    stopRecordBtn.addEventListener('click', function(e) {
     
            fileRecorded = audioRecorder.stop();  
            newFile = Titanium.Filesystem.getFile(Ti.Filesystem.getExternalStorageDirectory,"hello.wav");
            newFile.write(fileRecorded);
            var toUpload = newFile.read();
     
            var xhr = Titanium.Network.createHTTPClient();
            xhr.setTimeout(15000);
            xhr.onerror = function(e) {
                displayHTTPerror(e, this);
     
                if (callback) {
                    callback(false);
                }
            };
     
            xhr.onload = function(e){
                    if (this.status === 200) {
                        var response = this.responseData;
                        alert(response);
                    }else{
                        displayHTTPerror(e, this);
                    }
     
            };
            //xhr.setTimeout ( 20000 );
            xhr.open('POST', 'http://example.com/appupload', false);
            xhr.send({media: toUpload});
     
    });

    — commented 7 months ago by Ned Turner

Your Answer

Think you can help? Login to answer this question!