在Phonegap中从Android的图库中选择时无法加载图像 [英] Unable to load image when selected from the gallery on Android in phonegap

查看:90
本文介绍了在Phonegap中从Android的图库中选择时无法加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从android画廊中选择图像并将图像上传到服务器.我的代码对IOS应用程序正常工作.但无法从Android手机获取图像.

I want to select image form android gallery and upload image to server. My code works properly for IOS apps. But not getting image from android phone.

内部android应用获取这样的显示图片网址

Inside android app getting show image url like this

content://com.android.providers.media.documents/document/image%3A352

content://com.android.providers.media.documents/document/image%3A352

我应该怎么做才能从Android Gallery获得正确的图像扩展名? 我正在使用带Phonegap的Hybird App.

What should I do to get the right image extension from Android gallery? I am using Hybird App with Phonegap.

document.addEventListener("deviceready", onDeviceReady, false);

        // device APIs are available
        //
        function onDeviceReady() {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(
                uploadPhoto,
                function(message) { alert('get picture failed'); },
                {
                    quality         : 100,
                    destinationType : navigator.camera.DestinationType.FILE_URI,
                    sourceType      : navigator.camera.PictureSourceType.PHOTOLIBRARY
                }
            );
        }

        function uploadPhoto(imageURI) {
            //alert(imageURI); return false;

            //alert(imageURI);  

            var options = new FileUploadOptions(); 
            options.fileKey="file";

            //options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
            options.mimeType="image/jpeg";

            var params = {};
            params.value1 = "test";
            params.value2 = "param";

            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.params = params; 
            options.chunkedMode = true; //this is important to send both data and files

            alert(options.fileName);
            var ft = new FileTransfer();
            ft.upload(imageURI, encodeURI("file_upload.php"), win, fail, options);
        }

        function win(r) {
                alert("Code = " + r.responseCode); //http response 200, means OK
                alert("Response = " + r.response); //php uploader response (failed)
                alert("Sent = " + r.bytesSent); //filesize
       }

        function fail(error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }

推荐答案

这是Cordova错误-

This is a cordova bug - https://issues.apache.org/jira/browse/CB-5398.

您可以像这样更改路径.

You can change your path like this.

function uploadPhoto(imageURI) {
            //alert(imageURI); return false;

            //alert(imageURI);  
            if (imageURI.substring(0,21)=="content://com.android") {
                photo_split=imageURI.split("%3A");
                imageURI="content://media/external/images/media/"+photo_split[1]; 
              } 

            var options = new FileUploadOptions();
            options.fileKey="file";
            //options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
            options.mimeType="image/jpeg";

            var params = {};
            params.value1 = "test";
            params.value2 = "param";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.params = params; 
            options.chunkedMode = true; //this is important to send both data and files

            //alert(options.fileName);
            var ft = new FileTransfer();
            ft.upload(imageURI, encodeURI("file_upload.php"), win, fail, options);
        }

并将图像请求发送到file_upload.php.添加这样的条件.

And send image request to file_upload.php. Add some condition like this.

       $file=$_FILES['file']['name']; 
        $new_file=explode('.', $file);
        $img_file=end($new_file);

        if($img_file=='png' || $img_file=='jpeg' || $img_file=='jpg')
        {  
            $file_name=$new_file[0];
        }  else { 
             $file_name=$_FILES['file']['name'];
             $_FILES['file']['name']=$_FILES['file']['name'].'.jpg';
        } 

        move_uploaded_file($_FILES["file"]["tmp_name"], 'your_location'.$file_name);

这篇关于在Phonegap中从Android的图库中选择时无法加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆