jquery-mobile - Phonegap 相机插件问题(DestinationType.FILE_URI)

标签 jquery-mobile cordova cordova-plugins

我正在开发一个基于 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, 0, 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”函数时,“获取图片”窗口打开,我可以选择图像文件。然后执行 attachSuccess 回调函数。但是我无法使用 FILE URI 访问该文件。 FILE URI 打印如下,

content://media/external/images/media/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 相机插件问题(DestinationType.FILE_URI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27266371/

相关文章:

jquery mobile 如何切换选择的显示

javascript - HTML5 和分贝计

ajax - PhoneGap ajax 调用每次都失败

android - 我们如何在 cordova inappbrowser 中配置代理

ios - HTML5 音频回调在 safari/iOS 上失败

html - JQuery Mobile 更改页面上所有元素的字体大小

javascript - 从函数内部调用外部函数 - Javascript

javascript - Cordova 应用程序中的 Hammer.js 事件仅在第二次尝试时触发

ios - 无法更改 $cordovaStatusbar 文本颜色

java - 后台服务中的 sensorManager.registerListener