jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery-mobile – Phonegap Camera插件问题(DestinationType.FILE_URI)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个基于phonegap的应用程序,它具有文件传输功能.我正在使用phonegap相机插件来选择图像文件.该代码适用于’DesTinationType.DATA_URL’.但我无法使用’DesTinationType.FILE_URI’访问该文件.

DesTinationType.DATA_URL只提供图像文件内容.但我必须得到图像文件名称文件路径及其内容.所以我必须在相机选项中使用’DesTinationType.FILE_URI’.以下是我的代码,

function attachFile() {
 var picturesource=navigator.camera.PicturesourceType;
 var cameraOptions = { quality: 49,desTinationType:
 Camera.DesTinationType.FILE_URI,sourceType: picturesource.PHOTOLIBRARY };             
 navigator.camera.getPicture(attachsuccess,attachFail,cameraOptions);
}  

function attachsuccess(fileuri) {
 filePath = JSON.Stringify(fileuri);
 console.log("FilePath: "+filePath ); 
 window.requestFileSystem(LocalFileSystem.PERSISTENT,gotFS,fail); 
}

function attachFail() {
 console.log("attach Failed");
}

function gotFS(fileSystem) {            

  console.log("gotFS:");      
  var root = "/"+fileSystem.root.name;  
  console.log("root:  "+root); 
  filePath = filePath .subString(filePath.indexOf(root));        

  var imagename = filePath.subString(filePath.lasTindexOf('/'));
  var type = imagename.subString(filePath.indexOf('.'));
  fileSystem.root.getFile(filePath,null,gotFileEntry,fail);  

}

function fail() {
  console.log("** Failed **");   
}

function gotFileEntry(fileEntry) {
  console.log("got file entry");
  fileEntry.file(gotFile,fail);
}

function gotFile(filE) {
  console.log("got file");       
}

当我调用’attachFile’函数时,Get Picture窗口打开&我可以选择图像文件.然后执行attachsuccess回调函数.但是我无法使用FILE URI访问该文件. FILE URI打印如下,

内容://媒体/外部/图像/媒体/ 5490

我想知道如何从这个URI中获取文件名’或’文件对象’.请建议.
(在Android Kitkat& Lollipop中测试的代码)

解决方法

您可以使用COrdova的FileSystem API.这是一个如何加载图像文件快速例:

window.resolveLocalFileSystemURL(filePath,// The file path
    function(fileEntry) { // found the file
        fileEntry.file(function(f) { // got the file
            var reader = new FileReader();

            reader.onloadend = function(evt) {
                var fileContent = reader.result;

                // Do something with the fileContent
                someImage.attr("src",fileContent); // Example
            }

            reader.readAsDataURL(f); // TODO find a way to read it sTraight to the right format (if there is any)
        },function(E) { // cAnnot open file
            console.log("Error opening file: " + e.messagE);
        });
    },function(E) { // file not found
        console.log("Error finding file: " + e.messagE);
    });

fileContent是一个包含’data:’base64编码字符串的字符串.
您还可以使用f变量来获取文件名.

文献:

https://developer.mozilla.org/en-US/docs/Web/API/FileReader.readAsDataURL

https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

大佬总结

以上是大佬教程为你收集整理的jquery-mobile – Phonegap Camera插件问题(DestinationType.FILE_URI)全部内容,希望文章能够帮你解决jquery-mobile – Phonegap Camera插件问题(DestinationType.FILE_URI)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。