gpt4 book ai didi

javascript - 在 Firefox 操作系统中将图像文件保存到 SD 卡中

转载 作者:行者123 更新时间:2023-11-28 19:55:22 24 4
gpt4 key购买 nike

我正在尝试将图像保存在 SD 卡中。我正在关注this文档。

$('.btnSave').on('click', function () {

var imageRawData = canvas.toDataURL("image/png") ;
var sdcard = navigator.getDeviceStorage("sdcard");
var file = new Blob([imageRawData], { type: "image/png" });
var request = sdcard.addNamed(file, "FilertedImage.png");

request.onsuccess = function () {
var name = this.result;
console.log('File "' + name + '" successfully wrote on the sdcard storage area');

}
request.onerror = function (e) {
console.log('Unable to write the file: ' + e.target.error.name);

}
});

在文档中,我发现“图片仅接受具有有效 image mime type 的 Blob”。那么如何使用 javascript 将 imageRawData 转换为有效的图像 MIME 类型。

最佳答案

我已经这样做了,如下 - 保存然后共享:

 function saveAndSend(blob) {
var sdcard = navigator.getDeviceStorage("sdcard");
var request = sdcard.addNamed(blob, "test/mycanvas.png");

//could just share the blob instead of saving
request.onsuccess = function () {
var sharingImage = new MozActivity({
name: "share",
data: {
type: "image/*",
number: 1,
blobs: [blob],
filenames: ["mycanvas.png"],
filepaths: ["test/mycanvas.png"]
}
});
}

// An error could occur if a file with the same name already exist
request.onerror = function () {
alert('Unable to write the file: ' + this.error.name);
}

}


var cnv = document.getElementById('myCanvas');
cnv.toBlob(function (blob) {

//var sdcard = navigator.getDeviceStorage("pictures");
var sdcard = navigator.getDeviceStorage("sdcard");
var request = sdcard.delete("test/mycanvas.png");
//try to delete in case it exists
request.onsuccess = function () {
saveAndSend(blob);
}

request.onerror = function () {
saveAndSend(blob);
}


});

关于javascript - 在 Firefox 操作系统中将图像文件保存到 SD 卡中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com