【发布时间】:2015-01-31 16:48:20
【问题描述】:
我正在开发一个基于 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 中测试的代码)
【问题讨论】:
-
“访问文件”是什么意思?你想把它的数据作为二进制流或类似的东西吗?
标签: jquery-mobile cordova cordova-plugins