gpt4 book ai didi

android - 在 phonegap 中从 Android 上的图库中选择时无法加载图像

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:03 24 4
gpt4 key购买 nike

我想从安卓图库中选择图片并将图片上传到服务器。我的代码适用于 IOS 应用程序。但无法从 Android 手机获取图像。

在 android 应用程序中获取这样的显示图像 url

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

我应该怎么做才能从 Android 画廊获得正确的图像扩展?我正在将 Hybird App 与 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 错误 - https://issues.apache.org/jira/browse/CB-5398 .

你可以像这样改变你的路径。

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。添加这样的条件。

       $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);

关于android - 在 phonegap 中从 Android 上的图库中选择时无法加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com