我想提供一种方便的方法将数据从我的Cordova应用程序备份到用户。对我来说最吸引人的是我的应用程序会将所有数据备份到JSON文件中,然后让用户以与任何其他文件相同的方式保存/共享它。
我已将数据保存到真实文件并调用共享对话框,但该文件无法共享/保存。这可能是因为外部应用程序无权访问我的应用程序创建的文件(GMail说:'对于附件的权限被拒绝')我不知道如何允许外部应用访问我的应用创建的文件。有什么想法吗?
我的代码:
ex = JSON.stringify(ex);
window.resolveLocalFileSystemURL('cdvfile://localhost/temporary/', function(dir)
{
console.log('Directory resolved.');
dir.getFile('MakeShift.json', {create: true, exclusive: false}, function(file)
{
console.log('Date file created.');
file.createWriter(function (fileWriter)
{
fileWriter.onwriteend = function()
{
console.log('Data saved.');
var time = Date.now();
console.log(file); // typeof FileEntry
window.plugins.socialsharing.shareWithOptions({
message: 'My MakeShift backup from '+time,
subject: 'MakeShift Backup '+time.yyyymmdd(),
files: [file.nativeURL],
chooserTitle: 'Choose how to save your data'
},
function(result)
{
file.remove(function (file)
{
console.log('Data file removed.');
});
});
};
fileWriter.onerror = function(e)
{
console.log('There was an error writing the data file.', e);
};
fileWriter.write(new Blob([ex], { type: 'application/json' }));
});
},
function(err)
{
console.log('There was an error creating the data file.', err);
});
});