Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-8023] Android: Unable to pass variable to webview from successful showCamera using evalJS

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-04-19T16:07:14.000+0000
Affected Version/sRelease 1.8.0.1, Release 1.8.1, Release 1.8.2
Fix Version/sRelease 2.0.0, Sprint 2012-06
ComponentsAndroid
Labelsmodule_webview, qe-testadded
ReporterNikhil Sharma
AssigneeOpie Cyrus
Created2012-03-14T15:51:56.000+0000
Updated2012-11-16T22:52:07.000+0000

Description

Unable to pass the path of the picture that was taken by the camera to the webview using evalJS. It results in the crash of the app. This works fine in iOS.

Repro Steps

1. Run the below code. 2. Click the Take Picture button. It will open the camera. 3. Take the picture and click Ok button of the camera. It will crash the app.

Expected behavior: Alert with the image path should pop up.

var tabGroup = Titanium.UI.createTabGroup();
 
var win4 = Titanium.UI.createWindow({
    title: "HTML",
    url:"windows/win4.js"
});
var tab4 = Titanium.UI.createTab({
    title: "HTML",
    window: win4
}); 
 
tabGroup.addTab(tab4);
tabGroup.open();
var win = Titanium.UI.currentWindow;
var webview = Titanium.UI.createWebView({url:'win4.html'});
win.add(webview);
 
Ti.App.addEventListener('showCamera', function() {
    Titanium.Media.showCamera({ 
        success:function(event) 
        {
            var imageView = Ti.UI.createImageView({image:event.media});
            var cropRect = event.cropRect;
            var image = event.media;
            var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'photo.png');
            file.write(image);
            var data = file.nativePath;
            //alert(data);
            webview.evalJS('imgPass("' + data + '");');
        },
        cancel:function()
        {
        },
        error:function(error)
        {
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                a.setMessage('Please run this test on device');
            }
            else
            {
                a.setMessage('Unexpected error: ' + error.code);
            }
            a.show();
        },
        mediaTypes:Ti.Media.MEDIA_TYPE_PHOTO,
        saveToPhotoGallery:true,
        allowEditing:true
    });
});
<html>
    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
        <style type="text/css">
            body { text-align: center; font-size: 2em; } #button { display: inline-block; background-color: #000; color: #FFF; font-weight: bold; margin: 20px 0 0; padding: 10px 20px; }
        </style>
        <script type="text/javascript">
            function Init() {
                document.getElementById("button").addEventListener('click', function(e) {
                    Ti.App.fireEvent('showCamera');
                });
            } 
            function imgPass(path) {
                alert(path);                
            }
        </script>
    </head>
    <body onload="Init()"> 
        <span id="button">
            Take Picture        
        </span> 
    </body>
</html>

Comments

  1. Eric Merriman 2012-03-19

    Verified fixed with 2.0.0.v20120319003254 and Titanium Studio, build: 2.0.0.201203182248. Image was captured and alert presented on Nexus S 2.3.6.
  2. Wilson Luu 2012-04-19

    Reopening to update labels.

JSON Source