在phonegap中从Android上的图库中选择时无法加载图像
我想从android gallery中选择图像并将图像上载到服务器。我的代码适用于IOS应用程序。但无法从android手机上获取图像 在android应用程序中,获得如下显示图像url content://com.android.providers.media.documents/document/image%3A352 我应该怎么做才能从Android gallery获得正确的图像扩展? 我正在使用Phonegap的Hybird应用程序在phonegap中从Android上的图库中选择时无法加载图像,android,cordova,phonegap-plugins,phonegap-build,android-gallery,Android,Cordova,Phonegap Plugins,Phonegap Build,Android Gallery,我想从android gallery中选择图像并将图像上载到服务器。我的代码适用于IOS应用程序。但无法从android手机上获取图像 在android应用程序中,获得如下显示图像url content://com.android.providers.media.documents/document/image%3A352 我应该怎么做才能从Android gallery获得正确的图像扩展? 我正在使用Phonegap的Hybird应用程序 document.addEventListener("
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);
}
您可以指定encodingType:Camera.encodingType.JPEG并在成功回调中呈现图像,如下所示-$'yourElement.attr'src',数据:image/JPEG;base64,+图像数据
这是一只科尔多瓦虫子
你可以像这样改变你的道路
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);
}
并将图像请求发送到文件_upload.php。添加一些这样的条件
$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);
这是有益的。。。谢谢
$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);