我成功地创建了目录,如果有必要,我想在我的设备上的Android文件系统中创建一个Moto X 1st gen。我成功保存了纯文本文件,但无法保存jpg文件。我有一个base64编码的图像字符串,它在我的img标签中显示src =“data:image / jpeg; base64,/ 9j / 4AAQSkZJRgABAQAAAQ ...”。在下面的第17行,我写了一个文件,其中“data:image / jpeg; base64”替换为“”(空字符串)。在写入文件之前是否有转换为二进制或我需要做的事情?我在notepad ++中打开了下面用代码片段编写的文件,我看到了很长的base64编码字符串。任何帮助将不胜感激。抱歉,我无法提供完整的代码,因为它非常大。我知道我的代码可以写文件,但是图像文件没有正确写入。谢谢!
var wpt = '01';
imageName = wpt + imageName;
var geoJSON = '{"type":"FeatureCollection","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::3857"}},"features":[{"type":"Feature","properties":{"TYPE":"WAYPOINT","IDENT":"null","LAT":' + latitude + ',"LONG":' + longitude + ',"Y_PROJ":null,"X_PROJ":null,"COMMENT":"' + createDate + ' ' + createTime + '","DISPLAY":"null","SYMBOL":"null","UNUSED1":"null","DIST":null,"PROX_INDEX":"null","COLOR":"null","ALTITUDE":null,"DEPTH":null,"TEMP":null,"TIME":"null","WPT_CLASS":"null","SUB_CLASS":null,"ATTRIB":"null","LINK":null,"STATE":null,"COUNTRY":null,"CITY":null,"ADDRESS":null,"FACILITY":null,"CROSSROAD":null,"UNUSED2":"null","ETE":"null","DTYPE":"null","MODEL":"SUNSAIL","FILENAME":"' + description + '","LTIME":"null","WPT":"' + wpt + '"},"geometry":{"type":"Point","coordinates":[' + coordinates + ']}}]}';
root.getFile(gisPath + '/' + gisName, { create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function(evt) {
root.getFile(photoPath + '/' + imageName, { create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function(evt) {
if (i == (numImages - 1)) {
alert('Waypoint data and images saved succesfully for later upload.');
clearCache();
clearNew();
$('.uploader').remove();
}
}
writer.write(img.src.replace(/data:image\/jpeg;base64,/, ''));
}, function(evt) {
alert('Error code: ' + evt.target.error.code + '\n' + 'Error message: ' + evt.target.error.message);
});
}, function(evt) {
alert('Error code: ' + evt.target.error.code + '\n' + 'Error message: ' + evt.target.error.message);
});
}
writer.write(geoJSON);
}, function(evt) {
alert('Error code: ' + evt.target.error.code + '\n' + 'Error message: ' + evt.target.error.message);
});
}, function (evt) {
alert('Error code: ' + evt.target.error.code + '\n' + 'Error message: ' + evt.target.error.message);
});
答案 0 :(得分:0)
我用
替换了第17行writer.write(b64toBlob(img.src.replace(/data:image\/jpeg;base64,/, ''), 'image/jpg', 512));
这是我的b64toBlob功能
function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
这适用于将jpg图像保存到本地Android文件系统。