2

我想问一下你们激活showCamera并在android中打开钛的照片库功能时是否有人遇到问题。它将使应用程序崩溃并自动重新启动应用程序。我搜索了很多论坛,尤其是 JIRA appcelator 和钛论坛,但大多数情况都没有解决。我在论坛里试过很多方法,但还是一样。我想知道这是钛的虫子吗?

4

2 回答 2

1

从相机或图库中选择的最佳代码

//Create a dialog with options
var dialog = Titanium.UI.createOptionDialog({
    //title of dialog
    title: 'Choose an image source...',
    //options
    options: ['Camera','Photo Gallery', 'Cancel'],
    //index of cancel button
    cancel:2
});
 
//add event listener
dialog.addEventListener('click', function(e) {
    //if first option was selected
    if(e.index == 0)
    {
        //then we are getting image from camera
        Titanium.Media.showCamera({
            //we got something
            success:function(event)
            {
                //getting media
                var image = event.media; 
                 
                //checking if it is photo
                if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
                {
                    //we may create image view with contents from image variable
                    //or simply save path to image
                    Ti.App.Properties.setString("image", image.nativePath);
                }
            },
            cancel:function()
            {
                //do somehting if user cancels operation
            },
            error:function(error)
            {
                //error happend, create alert
                var a = Titanium.UI.createAlertDialog({title:'Camera'});
                //set message
                if (error.code == Titanium.Media.NO_CAMERA)
                {
                    a.setMessage('Device does not have camera');
                }
                else
                {
                    a.setMessage('Unexpected error: ' + error.code);
                }
 
                // show alert
                a.show();
            },
            allowImageEditing:true,
            saveToPhotoGallery:true
        });
    }
    else if(e.index == 1)
    {
        //obtain an image from the gallery
        Titanium.Media.openPhotoGallery({
            success:function(event)
            {
                //getting media
                var image = event.media; 
                // set image view
                 
                //checking if it is photo
                if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
                {
                    //we may create image view with contents from image variable
                    //or simply save path to image
                    Ti.App.Properties.setString("image", image.nativePath); 
                }   
            },
            cancel:function()
            {
                //user cancelled the action fron within
                //the photo gallery
            }
        });
    }
    else
    {
        //cancel was tapped
        //user opted not to choose a photo
    }
});
 
//show dialog
dialog.show();

谢谢

于 2015-02-07T21:58:26.780 回答
0

我正在使用此代码打开相机,它可以正常工作:

    Titanium.Media.showCamera({

        success:function(event)
        {
            //getting media
            var image = event.media;
            im=image;
            //checking if it is photo
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
            {
                imgpath= image.nativePath;
                $.userphoto.image=imgpath;//$.userphoto is an imageview
            }
        },
        cancel:function()
        {
            //do somehting 
        },
        error:function(error)
        {
            //error happened,
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
            //set message
            if (error.code == Titanium.Media.NO_CAMERA)
            {
                alert('No Cam');
            }
            else
            {
                alert('error');
            }

        },
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
    });
}

这是我用来打开照片库的代码(也可以正常工作):

Titanium.Media.openPhotoGallery({
        success:function(event)
        {

            //check if  photo
            if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
            {   
                im=resize(event.media); //just a function to resize the photo
                imgpath= event.media.nativePath;

                $.userphoto.image=imgpath;
            }  
        },
        cancel:function()
        {
            //user cancelled 
        }
    });

顺便说一句,我现在使用的是 Titanium SDK 3.4.1 GA,但它也曾经在 3.1.3GA 上工作

于 2015-02-07T20:22:39.860 回答