【问题标题】:Access image files on Android device using phonegap framework使用 phonegap 框架访问 Android 设备上的图像文件
【发布时间】:2015-12-16 01:39:29
【问题描述】:

我正在使用 Phonegap 5.3.1 和 Android 22。我正在尝试使用 phonegap 的 Filetransfer api 将图像传输到远程服务器。

var options = new FileUploadOptions();          
options.fileKey="file";
options.fileName=imageData.substr(imageData.lastIndexOf('/')+1);
alert("The filename here is : " + imageData);

所以这里会打印 imageData

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

还有下面的代码

imageData.substr(imageData.lastIndexOf('/')+1);

是否分配 options.fileName = "image%3A53"

这不是我在设备上的图像名称。在我看来,这导致了一个问题。

用于运行 FileTransfer 插件的 javascript 表示文件已成功传输,但我在服务器上找不到该文件(运行 PHP)。

服务器响应如下:

得到服务器的响应

  Array
  (
      [file] => Array
          (
              [name] => image%3A53
              [type] => image/jpeg
              [tmp_name] => /tmp/php8qjZXN
              [error] => 0
              [size] => 137490
          )
  )

也有少数帖子说: Phonegap android unable to upload image using fileTransfer

您不能使用 FileTransfer 上传方法中从相机成功回调中获得的 imageUri,您必须首先将 uri 解析为这样的文件名:

navigator.camera.getPicture(function(imageURI){

      window.resolveLocalFileSystemURI(imageUri, function(fileEntry) {
            fileEntry.file(function(fileObj) {

                var fileName = fileObj.fullPath;

                //now use the fileName in your method
                //ft.upload(fileName ,serverURL + '/ajax.php?fname=appuploadspotimage'...);

            });
        });
});

任何帮助将不胜感激。

【问题讨论】:

  • 137490 字节是正确的文件大小吗?如果是这样,我会说您的服务器正在获取文件。你在/tmp/php8qjZXN 发现了什么?
  • 文件大小似乎正确。我还得到以下 -bash: cd: /tmp/php8qjZXN: No such file or directory
  • 我们能看到处理请求并存储文件的 PHP 吗?
  • 看起来是不是 PHP 脚本出错了?

标签: javascript android phonegap-plugins


【解决方案1】:

我找到了我的问题的答案。问题不在客户端,而是在服务器端。 我参考了以下帖子: fileupload success, but no photos in the server (phonegap android)

关键是“你检查过你的文件夹和脚本权限了吗?他们都应该有相同的用户/组。如果你正在运行 Apache,它会像 apache:apache。从 cmd 行你可以运行 chown apache:apache upload.php 然后 chown -R apache:apache php/"

函数 move_upload_file([source],[destination])。您的图像文件夹的路径应该在您的网站根目录下。上传的文件从 /tmp 移动到您的上传文件夹。该文件夹(和执行工作的脚本)需要归 apache 所有。

我按照上面提到的方式获得了权限,并且成功了。感谢大家的及时回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多