我有ionic 4应用程序,可以在电池连接更好的情况下拍照并保存到该应用程序中以供将来上传。我已经成功拍摄了照片,现在正在尝试使用iOS设备上的文件进行此操作。我正在使用IOSFilePicker插件获取uri链接,以将该文件的副本复制到tmp文件夹中,然后将其复制到目录中。文件失败。我认为这与uri中的private有关
copyFileToLocalDir namePath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox /
成功保存照片的日志
createFileName newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpg
copyFileToLocalDir名称路径文件:/// var / mobile / Containers / Data / Application / 49F98F61-0CD7-4F13-A8AB-F9068B1D924B / tmp /
copyFileToLocalDir currentName cdv_photo_001.jpg
copyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpgcopyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602250030.jpg
失败日志
getFile currentName photo2.jpg
getFilecorrectPath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox /
createFileNameAlt newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602196747
copyFileToLocalDir namePath /private/var/mobile/Containers/Data/Application/49F98F61-0CD7-4F13-A8AB-F9068B1D924B/tmp/com.complyworksmobile.app-Inbox /
copyFileToLocalDir currentName photo2.jpg
copyFileToLocalDir newFileName UUID_a612e7a5-6660-4c2e-901d-3e5be0715eec_questionid_UUID16_time_1588602196747
err {“ code”:5,“ message”:“ ENCODING_ERR”}
我能看到的唯一真正的区别是namePath包含/ private /。这两个文件都是.jpg照片。如果存在问题,是否有办法绕过此/ private /。
我的代码如下
ts。
getFile() {
this.filePicker.pickFile()
.then((uri) => {
console.log('uri', uri);
var currentName = uri.substr(uri.lastIndexOf('/') + 1);
var correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
console.log("getFile currentName ", currentName);
console.log("getFile correctPath ", correctPath);
this.copyFileToLocalDir(correctPath, currentName, this.createFileNameAlt());
}
)
.catch(err => console.log('Error', err));
}
createFileName() {
var d = new Date(),
n = d.getTime(),
f = this.formID,
q = this.sectionUUID,
newFileName = "UUID" + "_" + f + "_" + "questionid_" + q + "_" + "time_" + n + ".jpg";
console.log("createFileName newFileName ", newFileName);
return newFileName;
}
createFileNameAlt() {
var d = new Date(),
n = d.getTime(),
f = this.formID,
q = this.sectionUUID,
newFileName = "UUID" + "_" + f + "_" + "questionid_" + q + "_" + "time_" + n;
console.log("createFileNameAlt newFileName", newFileName);
return newFileName;
}
copyFileToLocalDir(namePath, currentName, newFileName) {
console.log("copyFileToLocalDir namePath", namePath);
console.log("copyFileToLocalDir currentName", currentName);
console.log("copyFileToLocalDir newFileName", newFileName);
this.file.copyFile(namePath, currentName, this.file.dataDirectory, newFileName).then(success => {
this.updateStoredImages(newFileName);
console.log("copyFileToLocalDir newFileName ", newFileName);
}, error => {
let err = JSON.stringify(error);
console.log("err", err);
this.presentToast('Error while storing file.');
});
}
结果是/ private /是问题。最终要做的是将/ private /切掉,并在其位置添加file://,然后运行良好]
下面是我编辑的getFile()
getFile() {
this.filePicker.pickFile()
.then((uri) => {
console.log('uri', uri);
var currentName = uri.substr(uri.lastIndexOf('/') + 1);
var correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
let newName = correctPath.slice(8);
let AmendLink = "file://" + newName;
this.copyFileToLocalDir(AmendLink, currentName, this.createFileNameAlt());
}
)
.catch(err => console.log('Error', err));
}