Choose camera in file upload in cordova application on android without using cordova camera

前端 未结 6 2072
野趣味
野趣味 2021-01-07 17:01

So i made a cordova app, i added android platform and made a simple html with an imput field



        
6条回答
  •  死守一世寂寞
    2021-01-07 17:31

    Here's another work around if you don't want to modify plugin source code. Create a separate control for taking pictures. Set the control's click event to the following handler:

    (event) => {
        event.stopPropagation();
        Camera.sourceType = Camera.PictureSourceType.CAMERA;
    
        const onCameraSuccess = (imgURL) => {
            window.resolveLocalFileSystemURL(imgURL, (entry) => {
                const onFileSuccess = (file) => this._onSelectMultipleFiles(event, file);
                const onFileFail = (error) => console.log(error);
                entry.file(onFileSuccess, onFileFail);
            });
    
            console.log("picture retrieved successfully");
        };
    
        const onCameraFail = () => {
            console.log("picture retrieval failed");
        };
    
        navigator.camera.getPicture(onCameraSuccess, onCameraFail, {
            quality: 100, 
            destinationType: Camera.DestinationType.FILE_URI,
        });
    }
    

    This uses cordova-plugin-camera to launch the camera app when the control is clicked, and calls resolveLocalFileSystemURL from cordova-plugin-file to convert the image URL returned by the camera into a File object to be processed by my _onSelectMultipleFiles method. see example from cordova docs

    Note that this implementation is specific to my project, so you may not need to call resolveLocalFileSystemURL depending on how you intend to use the image URL passed in the onSuccess callback of camera.getPicture.

    Obviously the down side to this is the need to use two controls: one for retrieving images from file, the other for retrieving images from the camera.

提交回复
热议问题