Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const platform = require("platform");
- const fs = require("file-system");
- function getBase64String(file) {
- const data = file.readSync();
- if (platform.isIOS) {
- return data.base64EncodedStringWithOptions(0);
- }
- if (platform.isAndroid) {
- return android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP);
- }
- }
- openImagePicker () {
- console.log("OPENING IMAGE PICKER :)");
- var ref = this
- var context = imagepicker.create({ mode: "single" });
- context
- .authorize()
- .then(function() {
- debugger
- return context.present();
- })
- .then(function(selection) {
- debugger
- selection.forEach(function(selected) {
- // process the selected image
- debugger
- if (selected) {
- ref.getImageFilePath(selected).then(path => {
- console.log(`path: ${path}`);
- **********************************
- ref.uploadImage(path);
- **********************************
- debugger
- //this.uploadImage(path);
- });
- }
- });
- }).catch(function (e) {
- debugger
- // process error
- });
- },
- uploadImage(path) {
- let file = fs.File.fromPath(path);
- debugger
- console.log("************************************");
- const base64String = getBase64String(file);
- debugger
- this.files.push(base64String)
- debugger
- this.sendMessage()
- console.log("************************************");
- //const fileAsBlob = new Blob([file]);
- debugger
- },
- getImageFilePath(imageAsset) {
- return new Promise(resolve => {
- if (platform.isIOS) {
- const options = PHImageRequestOptions.new();
- options.synchronous = true;
- options.version =
- PHImageRequestOptionsVersion.Current;
- options.deliveryMode =
- PHImageRequestOptionsDeliveryMode.HighQualityFormat;
- PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
- imageAsset.ios,
- options,
- nsData => {
- // create file from image asset and return its path
- const tempFolderPath = fs.knownFolders
- .temp()
- .getFolder("nsimagepicker").path;
- const tempFilePath = fs.path.join(
- tempFolderPath,
- Date.now() + ".jpg"
- );
- nsData.writeToFileAtomically(
- tempFilePath, true);
- resolve(tempFilePath);
- }
- );
- } else {
- // return imageAsset.android, since it 's the path of the file
- resolve(imageAsset.android);
- }
- });
- },
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement