在我的应用中,用户可以从图库中选择图片,此图片将用于"用户详细信息"。因此,创建了一个屏幕,他存在一个名为" file"的输入文件。我需要将选择的图像复制到externalDataDirectory。
我的问题是如何从文件中选择URI来制作副本?
我的代码是......但每次都会收到错误代码9,但我不知道这是什么意思。
inputFile = $( '.file' ).val();
window.resolveLocalFileSystemURL( cordova.file.externalDataDirectory, function( myFileEntry ){
window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, function(fileEntry){
parentEntry = new DirectoryEntry( inputFile );
myFileEntry.getDirectory( "TRETA", {create: true, exclusive: false}, function(dir){
// copy the file to a new directory and rename it
fileEntry.root.copyTo( dir, "arquivo", function(entry){
console.log("New Path: " + entry.fullPath);
}, function(error){
console.log(error);
});
}, fail);
}, function(error){
console.log( error );
});
}, fail);
FFFFF
答案 0 :(得分:3)
(注意:假设您要复制位于app-root文件夹中的文件。请将其命名为appURI
)。
我只能为自己说话,但LocalFileSystem.PERSISTENT
使用我的android-phone(测试环境)指向file:///storage/emulated/0/
。使用此base-URI,默认情况下您没有读/写权限,但在其子文件夹中。
您要访问的文件夹应如下所示:file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/
(appURI
)
在此文件夹中,您已获得读/写权限。所以你可以使用自己的逻辑来自己获取这个网址,或者你也可以使用我的android-phone指向LocalFileSystem.TEMPORARY
的{{1}},所以我想它应该和你一样。
但总而言之:
file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/cache/
最后我们可以使用正确的function getAppURI(callback) {
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (filesystem) {
var cacheDir = filesystem.root.toURL();
var startPointCacheFolderName = cacheDir.match(/\/\w+\/$/) [0];
callback(cacheDir.replace(startPointCacheFolderName, '') + '/');
}, function (error) {
console.log('no access to app-filesystem');
}
);
}
:
appURI
希望这有帮助。
值得一提的是,错误代码并不总是表明操作无法成功的真正原因。使用此question时,还存在读/写权限错误以及错误代码(getAppURI(function (appURI) {
window.resolveLocalFileSystemURL(appURI, function (fileSystem) {
fileSystem.getFile('fileToCopy.txt', {
create: false, // try true first to make sure that there are no other problems around
exclusive: false
}, function (fileEntry) {
window.resolveLocalFileSystemURL(appURI+"NAME_OF_A_SUBFOLDER_YOU_WANT_TO_COPY_TO OTHERWISE_REPLACE_THIS_STRING", function (newFileEntry) {
fileEntry.copyTo(newFileEntry, 'CopiedFile.txt', function (result) {
console.log("save successfully:", result);
}, function (err) {
console.log("err-fileEntry.copyTo: ",err);
});
}, function (err) {
console.log("err-window.resolveLocalFileSystemURL: ",err);
});
}, function (err) {
console.log("err-fileSystem.getFile: ",err);
});
}, function (err) {
console.log("err-resolveLocalFileSystemURL: ",err);
});
});
)相同(查看注释)。