Skip to content

Instantly share code, notes, and snippets.

@engalar
Last active August 8, 2022 00:21
Show Gist options
  • Save engalar/45788259e5ebc4171ee4b5ef6e9d7820 to your computer and use it in GitHub Desktop.
Save engalar/45788259e5ebc4171ee4b5ef6e9d7820 to your computer and use it in GitHub Desktop.
cordova file集成
//https://stackoverflow.com/a/16245768
//const blob = b64toBlob(b64Data, contentType);
const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
//https://apidocs.rnd.mendix.com/9/client/mx.data.html#.saveDocument
mx.data.saveDocument("123456", "Bunnies.jpg", { width: 180, height: 180 }, fileBlob, function() {
// success
}, function(e) {
console.error(e);
});
mx.data.create({
entity: "MyFirstModule.Cat",
callback: function(obj) {
console.log("Object created on server");
console.log("my file document id", obj.id);
},
error: function(e) {
console.error("Could not commit object:", e);
}
});
const reader = new FileReader();
reader.onload = function(e) {
const blob = new Blob([new Uint8Array(e.target.result)], {type: file.type });
console.log(blob);
};
reader.readAsArrayBuffer(file);
document.addEventListener('deviceready', function () {
           var uri = "file:///storage/emulated/0/Android/data/com.saic.imaptest/cache/tp_5046214841926004852.amr";
           resolveLocalFileSystemURL(uri, function (entry) {
               entry.file(function (file) {
                   alert('my file is')
                   alert(file instanceof File)
               }, function (e) {
                   console.log('error getting file', e);
               });
           })
}, false);
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";
// BEGIN EXTRA CODE
// END EXTRA CODE
/**
 * http://mxdev.meicloud.com/mx-native-docs/?all=&id=138
 * 停止录音
 * @returns {Promise.<MxObject>}
 */
export async function stopRecord() {
    // BEGIN USER CODE
    return new Promise(function(resolve, reject) {
        window.cordova.exec(function(data) {
            mx.logger.info('stoprecord data=', data);
            ///storage/emulated/0/Android/data/com.saic.imaptest/cache/tp_1328830136345982488.amr
            mx.data.create({
                entity: "MideaCommon.audio",
                callback: function(mxObject) {
                    mx.logger.info('stoprecord mxObject.id=', mxObject.getGuid());
               
                    resolveLocalFileSystemURL(data, function(entry) {
                        mx.logger.info('stoprecord entry', typeof entry);
                        entry.file(function(file) {
                            const reader = new FileReader();
                            reader.onload = function(e) {
                                const blob = new Blob([new Uint8Array(e.target.result)], { type: file.type });
                                mx.logger.info('stoprecord save document blob', typeof blob);
                                mx.data.saveDocument(
                                    mxObject.getGuid(), file.name, {}, blob, function() {
                                        resolve(mxObject);
                                    }, function(e) {
                                        reject(e);
                                    }
                                );
                            };
                            reader.readAsArrayBuffer(file);
                        }, function(e) {
                            console.log('error getting file', e);
                            mx.logger.info('stoprecord get blob error', e);
                        });
                    }, e => mx.logger.info('stoprecord resolveLocalFileSystemURL error', e))
                },
                error: function(e) {
                    reject("Could create object:" + error.message);
                }
            });
        }, function(error) {
            mx.logger.error('stoprecord', error);
            reject("Error: " + error);
        }, "Audio", "stopRecord", []);
    });
    // END USER CODE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment