Image upload in android

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

Hi i have a problem while i upload the image from the android phone. I getting image name like "tixhr26727png". Please suggest what i doing wrong. Code:

Titanium.Media.openPhotoGallery({ success:function(event) { Ti.API.info("success! event: " + JSON.stringify(event)); var image = event.media;

        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'camera_photo.png');
        f.write(image);
        imageView.image = f.nativePath;
         var  data_to_send = { 
            "fileupload": image, 
            "name": 'camera_photo.png' 
        };

        xhr = Titanium.Network.createHTTPClient();
        xhr.open("POST","http://192.168.1.200/dinesh/image.php");


        xhr.send(data_to_send); 
        xhr.onload = function() {

            Ti.API.info(this.responseText); 
        };   


    },

    mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO],
    allowImageEditing: true

    });

2 Answers

Is there any reason why you want to take the chosen image and write it to the application data directory?

If you need to rename the file, it's a lot easier to do this on the php server..

Also, have you seen the xhr file upload example in KitchenSink?

See here

It's important to put the onload (and onerror, etc.) before calling open and send.

— answered 2 years ago by Kosso .
answer permalink
5 Comments
  • Also, you do should not put quotes around the names of the POST parameters in the data_to_send object.

    — commented 2 years ago by Kosso .

  • Hi Kosso,

    My main problem is that on php side i am not able to find the image name properly. I getting image name like "tixhr26727png", while i expecting it will be like "tixhr26727.png".

    Thanks in advanced

    — commented 2 years ago by dinesh singh

  • My Code:

    imageChooseLibrary.addEventListener('click', function(e) {

        Titanium.Media.openPhotoGallery({
        success:function(event)
        {
               Ti.API.info("success! event: " + JSON.stringify(event));
               var image1 = event.media;
    
    
    
            xhr = Titanium.Network.createHTTPClient();
            xhr.open("POST","http://192.168.1.200/dinesh/image.php");
            //xhr.setRequestHeader("enctype", "multipart/form-data");
            ///xhr.setRequestHeader("Content-Type", "image/png");
    
            xhr.send("fileupload":image1); 
            xhr.onload = function() {
               // textfield.value = this.responseText;
                Ti.API.info(this.responseText); 
            };   
    
    
        },
    
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO],
        allowImageEditing: true
    
        });
    

    });

    Please suggest what i am doing wrong.

    — commented 2 years ago by dinesh singh

  • Show 2 more comments

Try this :

Titanium.Media.openPhotoGallery({ 
 
    success:function(event) { 
 
        Ti.API.info("success! event: " + JSON.stringify(event)); 
 
        var image = event.media;
 
        // you don't really need to do this..
        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'camera_photo.png');
        f.write(image);
        // 
 
        // use the actual event.media (var image) for the imageview
        imageView.image = image;  
 
 
        var  data_to_send = { 
            fileupload: image,
            name: 'camera_photo.png' 
        };
 
 
 
        var xhr = Titanium.Network.createHTTPClient();
 
        // show an error, if one occurs...
        xhr.onerror = function(e){
            Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
            Ti.API.info('IN ERROR ' + e.error);
        };          
 
        // respond to a successful upload ...
        xhr.onload = function() {
            Ti.API.info(this.responseText); 
            // do stuff...
        }; 
 
        // how to show the progress if you need to ...
        xhr.onsendstream = function(e){
            Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
        };
 
        xhr.open('POST','http://192.168.1.200/dinesh/image.php');
 
        xhr.send(data_to_send); 
 
    },
 
    mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO],
    allowImageEditing: true
 
});

To get the image filename in your PHP, use something like this:

<?php
// PHP file upload ...
 
$destination_dir = "/some/writeable/directory/to/store/uploads";
 
 
$uploaded_filename = $_FILES['fileupload']['name']; // since your app is using the name 'fileupload', we must use that here too.. 
// obviously, you can use any name here for the desination filename, since mobile apps can create odd names
 
 
$upload_final_destination = $destination_dir."/".$uploaded_filename;
 
 
$response = Array(); // set this up to use later, to respond to the app.. (it will be converted to JSON later) 
 
 
// notice we use 'tmp_name' here which is the actual uploaded file.. 
 
if(!move_uploaded_file($_FILES['fileupload']['tmp_name'], $upload_final_destination)){
 
    $response['status'] = 'error';
    $response['error'] = 'unable to move file to '.$destination_dir;    
 
} else {
 
    $response['status'] = 'ok';
    $response['message'] = 'the file was uploaded to '.$upload_final_destination;
 
}
 
// now respond to the app using JSON...
 
echo json_encode($response);
 
exit;
 
?>

Your Answer

Think you can help? Login to answer this question!